Skip to content
Snippets Groups Projects
Commit 6f9ce7a7 authored by Jason R Wilson's avatar Jason R Wilson
Browse files

stuff

parent 47128343
Branches
Tags
No related merge requests found
This diff is collapsed.
This diff is collapsed.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv) {
MPI_Init (&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
char name[MPI_MAX_PROCESS_NAME];
int name_len;
MPI_Get_processor_name(name,&name_len);
printf ("name = %s\n",name);
printf ("Hello World! from MPI rank %d, number of ranks = %d\n",rank,size);
MPI_Finalize();
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include "vec.h"
int main (int argc, char* argv[]) {
// get num_threads from the command line
if (argc < 2) {
printf ("Command usage : %s %s\n",argv[0],"num_threads");
return 1;
}
int num_threads = atoi(argv[1]);
omp_set_num_threads(num_threads);
// read the number of points and the dimension of each point
int num_points, dim;
if (scanf("%*c %d %d",&num_points, &dim) != 2) {
printf ("error reading the number of points and the dimension\n");
return 1;
}
// read vectors from stdin and store them in a 2d array
double* data = (double*)malloc(num_points*dim*sizeof(double));
if (data == NULL) {
printf ("malloc return NULL pointer!\n");
return 1;
}
for (int i=0;i<num_points;i++) {
if (vec_read_stdin(data+i*dim,dim) != dim) {
printf ("error reading the next point from stdin\n");
return 1;
}
}
// start the timer
double start_time, end_time;
start_time = omp_get_wtime();
// find the extreme pair
double max_dist_sq = 0;
int extreme[2];
int pairs_checked = 0;
#pragma omp parallel default(none) shared(dim,data,pairs_checked,max_dist_sq,extreme,num_points,num_threads)
{
double thread_max_dist_sq = 0;
int thread_extreme[2];
int thread_pairs_checked = 0;
int thread_num = omp_get_thread_num();
#ifdef DYNAMIC
#pragma omp for schedule(dynamic)
#else
#pragma omp for schedule(static)
#endif
for (int i=0;i<num_points-1;i++) {
for (int j=i+1;j<num_points;j++) {
double dist_sq = vec_dist_sq(data+i*dim,data+j*dim,dim);
thread_pairs_checked += 1;
if (dist_sq > thread_max_dist_sq) {
thread_max_dist_sq = dist_sq;
thread_extreme[0] = i;
thread_extreme[1] = j;
}
}
}
#ifdef DEBUG
printf ("thread %d checked %d pairs\n",thread_num,thread_pairs_checked);
#endif
#pragma omp critical
{
pairs_checked += thread_pairs_checked;
if (thread_max_dist_sq > max_dist_sq) {
max_dist_sq = thread_max_dist_sq;
extreme[0] = thread_extreme[0];
extreme[1] = thread_extreme[1];
}
}
}
// stop the timer
end_time = omp_get_wtime();
// output the results
#ifdef TIMING
printf ("(%d,%.4f),",num_threads,end_time-start_time);
#else
printf ("num_threads = %d, ",num_threads);
printf ("elapsed time = %.6f seconds\n",end_time-start_time);
printf ("pairs checked = %d\n",pairs_checked);
printf ("Extreme Distance = %.2f\n",sqrt(max_dist_sq));
printf ("Extreme Pair = %d %d\n",extreme[0],extreme[1]);
#endif
// free memory allocated for dataset
free(data);
}
# make sure we are running on matrix!
if [ "$HOSTNAME" != matrix ]; then
printf "error : can only run this script on matrix!\n"
exit 1
fi
# check to make sure we provided a command line argument for the filename
if [ "$#" -ne 1 ]
then
echo "error: please specify filename as a command line argument"
exit 1
fi
# compile using TIMING flag
gcc -DTIMING -DDYNAMIC -O3 -o omp_extreme omp_extreme.c vec.c -lm -fopenmp
# run multiple times for scaling study
cat $1 | ./omp_extreme 1
cat $1 | ./omp_extreme 2
cat $1 | ./omp_extreme 4
cat $1 | ./omp_extreme 8
# print new line
printf '\n'
(1,1.5658),(2,0.7934),(4,0.3999),(8,0.2092),(16,0.1122),(32,0.0625)
\ No newline at end of file
(1,1.5626),(2,1.1806),(4,0.6941),(8,0.3816),(16,0.2039),(32,0.1162)
\ No newline at end of file
#!/bin/bash
#SBATCH -A cmda3634_rjh
#SBATCH -p normal_q
#SBATCH -t 10
#SBATCH --cpus-per-task=32
#SBATCH -o omp_extreme.out
# check to make sure we provided a command line argument for the filename
if [ "$#" -ne 1 ]
then
echo "error: please specify filename as a command line argument"
exit 1
fi
# Go to the directory where the job was submitted
cd $SLURM_SUBMIT_DIR
# Load the modules (including the gcc compiler!)
module load matplotlib
# compile using TIMING flag
gcc -DTIMING -DDYNAMIC -O3 -o omp_extreme omp_extreme.c vec.c -lm -fopenmp
# OpenMP settings
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
export OMP_PROC_BIND=true
# run multiple times for scaling study
cat $1 | ./omp_extreme 1
cat $1 | ./omp_extreme 2
cat $1 | ./omp_extreme 4
cat $1 | ./omp_extreme 8
cat $1 | ./omp_extreme 16
cat $1 | ./omp_extreme 32
#include <stdio.h>
// v = 0
void vec_zero (double v[], int dim) {
for (int i=0;i<dim;i++) {
v[i] = 0;
}
}
// read a vector from stdin
// returns the number of elements read in
int vec_read_stdin (double v[], int dim) {
for (int i=0;i<dim;i++) {
if (scanf("%lf",&(v[i])) != 1) { // could also use v+i
return i;
}
}
return dim;
}
// w = u + v
void vec_add (double u[], double v[], double w[], int dim) {
for (int i=0;i<dim;i++) {
w[i] = u[i] + v[i];
}
}
// w = cv
void vec_scalar_mult (double v[], double c, double w[], int dim) {
for (int i=0;i<dim;i++) {
w[i] = c*v[i];
}
}
// print a vector using the given format specifier
void vec_print (double v[], char* format, int dim) {
for (int i=0;i<dim;i++) {
printf (format,v[i]);
}
printf ("\n");
}
// calculate the norm squared of a vector
double vec_norm_sq (double v[], int dim) {
double norm_sq = 0;
for (int i=0;i<dim;i++) {
norm_sq += v[i]*v[i];
}
return norm_sq;
}
// calculate the distance squared between two vectors
double vec_dist_sq (double u[], double v[], int dim) {
double dist_sq = 0;
for (int i=0;i<dim;i++) {
dist_sq += (u[i]-v[i])*(u[i]-v[i]);
}
return dist_sq;
}
// performs the copy v->data[i] = w->data[i] for all i
void vec_copy (double v[], double w[], int dim) {
for (int i=0;i<dim;i++) {
v[i] = w[i];
}
}
#ifndef VEC_H
#define VEC_H
// v = 0
void vec_zero (double v[], int dim);
// read a vector from stdin
// returns the number of elements read in
int vec_read_stdin (double v[], int dim);
// w = u + v
void vec_add (double u[], double v[], double w[], int dim);
// w = cv
void vec_scalar_mult (double v[], double c, double w[], int dim);
// print a vector using the given format specifier
void vec_print (double v[], char* format, int dim);
// calculate the norm squared of a vector
double vec_norm_sq (double v[], int dim);
// calculate the distance squared between two vectors
double vec_dist_sq (double u[], double v[], int dim);
// performs the copy v->data[i] = w->data[i] for all i
void vec_copy (double v[], double w[], int dim);
#endif
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment