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

files for lecture19

parent 32878c66
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "vec.h"
int main (int argc, char* argv[]) {
// read the filename from the command line
if (argc < 2) {
printf ("command usage: %s %s\n",argv[0],"filename");
return 1;
}
char* filename = argv[1];
// open the text file for reading
FILE* fptr;
fptr = fopen(filename,"r");
// need to check for null
if (fptr == 0) {
printf ("Error opening data file %s.\n",filename);
exit(1);
}
// read the number of points and the dimension of each point
int num_points, dim;
if (fscanf(fptr,"%*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_file(fptr,data+i*dim,dim) != dim) {
printf ("error reading the next point from the file %s\n",filename);
return 1;
}
}
// close the data file
fclose(fptr);
// start the timer
clock_t start_time = clock();
// find the extreme pair
double max_dist_sq = 0;
int extreme[2];
int pairs_checked = 0;
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);
pairs_checked += 1;
if (dist_sq > max_dist_sq) {
max_dist_sq = dist_sq;
extreme[0] = i;
extreme[1] = j;
}
}
}
// stop the timer and calculate elapsed time
clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
// output the results
printf ("elapsed time = %.4f seconds\n",elapsed_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]);
// free memory allocated for dataset
free(data);
}
interact -A cmda3634_rjh -p normal_q -t 5 --ntasks-per-node=4
module load matplotlib
mpicc -o mpi_sum mpi_sum.c
mpiexec -n 4 ./mpi_sum 100000000
source diff could not be displayed: it is too large. Options to address this: view the blob.
source diff could not be displayed: it is too large. Options to address this: view the blob.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include "vec.h"
int main (int argc, char* argv[]) {
MPI_Init (&argc, &argv);
// MPI_COMM_WORLD is the default communicator that contains all ranks
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
char node_name[MPI_MAX_PROCESSOR_NAME];
int node_name_len;
MPI_Get_processor_name(node_name,&node_name_len);
// make sure we are not running on a login node!
if ((strcmp(node_name,"tinkercliffs1") == 0) ||
(strcmp(node_name,"tinkercliffs2") == 0)) {
printf ("error : running on login node %s!\n",node_name);
return 1;
}
// read the filename from the command line
if (argc < 2) {
printf ("command usage: %s %s\n",argv[0],"filename");
return 1;
}
char* filename = argv[1];
// open the text file for reading
FILE* fptr;
fptr = fopen(filename,"r");
// need to check for null
if (fptr == 0) {
printf ("Error opening data file %s.\n",filename);
exit(1);
}
// read the number of points and the dimension of each point
int num_points, dim;
if (fscanf(fptr,"%*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_file(fptr,data+i*dim,dim) != dim) {
printf ("error reading the next point from the file %s\n",filename);
return 1;
}
}
// close the data file
fclose(fptr);
// start the timer
double start_time, end_time;
start_time = MPI_Wtime();
// find the extreme pair
double max_dist_sq = 0;
int extreme[2];
int pairs_checked = 0;
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);
pairs_checked += 1;
if (dist_sq > max_dist_sq) {
max_dist_sq = dist_sq;
extreme[0] = i;
extreme[1] = j;
}
}
}
// stop the timer
end_time = MPI_Wtime();
// output the results
printf ("rank %d: elapsed time = %.4f seconds\n",rank,end_time-start_time);
printf ("rank %d: pairs checked = %d\n",rank,pairs_checked);
printf ("rank %d: Extreme Distance = %.2f\n",rank,sqrt(max_dist_sq));
printf ("rank %d: Extreme Pair = %d %d\n",rank,extreme[0],extreme[1]);
// free memory allocated for dataset
free(data);
MPI_Finalize();
}
#!/bin/bash
#SBATCH -A cmda3634_rjh
#SBATCH -p normal_q
#SBATCH -t 5
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH -o mpi_extreme.out
# Go to the directory where the job was submitted
cd $SLURM_SUBMIT_DIR
# Load the modules we need for MPI
module load matplotlib
# Build the executable
mpicc -o mpi_extreme mpi_extreme.c vec.c -lm
# Run mpi_extreme
printf 'Results with one rank:\n'
mpiexec -n 1 --map-by ppr:$SLURM_NTASKS_PER_NODE:node ./mpi_extreme $1
printf '\nResults with multiple ranks:\n'
mpiexec -n $SLURM_NTASKS --map-by ppr:$SLURM_NTASKS_PER_NODE:node ./mpi_extreme $1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include "vec.h"
int main (int argc, char* argv[]) {
MPI_Init (&argc, &argv);
// MPI_COMM_WORLD is the default communicator that contains all ranks
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
char node_name[MPI_MAX_PROCESSOR_NAME];
int node_name_len;
MPI_Get_processor_name(node_name,&node_name_len);
// make sure we are not running on a login node!
if ((strcmp(node_name,"tinkercliffs1") == 0) ||
(strcmp(node_name,"tinkercliffs2") == 0)) {
printf ("error : running on login node %s!\n",node_name);
return 1;
}
// read the filename from the command line
if (argc < 4) {
printf ("command usage: %s %s %s %s\n",argv[0],"filename","num_pairs","seed");
return 1;
}
char* filename = argv[1];
int num_pairs = atoi(argv[2]);
// seed the random number generator using command line seed
srandom(atoi(argv[3]));
// open the text file for reading
FILE* fptr;
fptr = fopen(filename,"r");
// need to check for null
if (fptr == 0) {
printf ("Error opening data file %s.\n",filename);
exit(1);
}
// read the number of points and the dimension of each point
int num_points, dim;
if (fscanf(fptr,"%*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_file(fptr,data+i*dim,dim) != dim) {
printf ("error reading the next point from the file %s\n",filename);
return 1;
}
}
// close the data file
fclose(fptr);
// start the timer
double start_time, end_time;
start_time = MPI_Wtime();
// find the approximate extreme pair
double max_dist_sq = 0;
int pairs_checked = 0;
int extreme[2];
for (int p=0;p<num_pairs;p++) {
int i = random() % num_points;
int j = random() % num_points;
double dist_sq = vec_dist_sq(data+i*dim,data+j*dim,dim);
pairs_checked += 1;
if (dist_sq > max_dist_sq) {
max_dist_sq = dist_sq;
extreme[0] = i;
extreme[1] = j;
}
}
// all nonzero ranks send their approximate extreme pair to rank 0
if (rank == 0) {
MPI_Status status;
int rank_extreme[2];
int rank_pairs_checked;
for (int src=1;src<size;src++) {
MPI_Recv(&rank_pairs_checked,1,MPI_INT,src,0,MPI_COMM_WORLD,&status);
pairs_checked += rank_pairs_checked;
MPI_Recv(rank_extreme,2,MPI_INT,src,0,MPI_COMM_WORLD,&status);
int i = rank_extreme[0];
int j = rank_extreme[1];
double dist_sq = vec_dist_sq(data+i*dim,data+j*dim,dim);
if (dist_sq > max_dist_sq) {
max_dist_sq = dist_sq;
extreme[0] = rank_extreme[0];
extreme[1] = rank_extreme[1];
}
}
} else {
int dest = 0;
MPI_Send(&pairs_checked,1,MPI_INT,dest,0,MPI_COMM_WORLD);
MPI_Send(extreme,2,MPI_INT,dest,0,MPI_COMM_WORLD);
}
// stop the timer
end_time = MPI_Wtime();
// output the results
if (rank == 0) {
printf ("elapsed time = %.4f seconds\n",end_time-start_time);
printf ("pairs checked = %d\n",pairs_checked);
printf ("Approximate Extreme Distance = %.2f\n",sqrt(max_dist_sq));
printf ("Approximate Extreme Pair = %d %d\n",extreme[0],extreme[1]);
}
// free memory allocated for dataset
free(data);
MPI_Finalize();
}
#!/bin/bash
#SBATCH -A cmda3634_rjh
#SBATCH -p normal_q
#SBATCH -t 00:10:00
#SBATCH --nodes=4
#SBATCH --ntasks-per-node=32
#SBATCH -o mpi_extreme_search.out
# Go to the directory where the job was submitted
cd $SLURM_SUBMIT_DIR
# Load the modules we need for MPI
module load matplotlib
# Build the executable
mpicc -O3 -o mpi_extreme_approx mpi_extreme_approx.c vec.c -lm
# run mpi_work on nodes*ntasks-per-node cores
mpiexec -n $SLURM_NTASKS --map-by ppr:$SLURM_NTASKS_PER_NODE:node ./mpi_extreme_approx $1 $2 $3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
MPI_Init (&argc, &argv);
// MPI_COMM_WORLD is the default communicator that contains all ranks
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
char node_name[MPI_MAX_PROCESSOR_NAME];
int node_name_len;
MPI_Get_processor_name(node_name,&node_name_len);
// make sure we are not running on a login node!
if ((strcmp(node_name,"tinkercliffs1") == 0) ||
(strcmp(node_name,"tinkercliffs2") == 0)) {
printf ("error : running on login node %s!\n",node_name);
return 1;
}
// get N from command line
if (argc < 2) {
printf ("Command usage : %s %s\n",argv[0],"N");
return 1;
}
long long N = atoll(argv[1]);
// start the timer
double start_time, end_time;
start_time = MPI_Wtime();
// calculate the sum
long long sum = 0;
for (long long i = 1+rank; i <= N;i+=size) {
sum += i;
}
// all nonzero ranks send their partial sums to rank 0
if (rank == 0) {
long long rank_sum;
MPI_Status status;
for (int src = 1;src < size;src++) {
MPI_Recv(&rank_sum,1,MPI_LONG_LONG,src,0,MPI_COMM_WORLD,&status);
sum += rank_sum;
}
} else {
int dest = 0;
MPI_Send(&sum,1,MPI_LONG_LONG,dest,0,MPI_COMM_WORLD);
}
// stop the timer
end_time = MPI_Wtime();
// print results
printf ("rank %d (of %d) sum = %lld, N*(N+1)/2 = %lld\n",rank,size,sum,(N/2)*(N+1));
if (rank == 0) {
printf ("elapsed time = %.4f seconds\n",end_time-start_time);
}
MPI_Finalize();
}
#!/bin/bash
#SBATCH -A cmda3634_rjh
#SBATCH -p normal_q
#SBATCH -t 5
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH -o mpi_sum.out
# Go to the directory where the job was submitted
cd $SLURM_SUBMIT_DIR
# Load the modules we need for MPI
module load matplotlib
# Build the executable
mpicc -o mpi_sum mpi_sum.c vec.c -lm
# Run mpi_sum
printf 'Results with one rank:\n'
mpiexec -n 1 --map-by ppr:$SLURM_NTASKS_PER_NODE:node ./mpi_sum $1
printf '\nResults with multiple ranks:\n'
mpiexec -n $SLURM_NTASKS --map-by ppr:$SLURM_NTASKS_PER_NODE:node ./mpi_sum $1
#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;
}
// read a vector from a file
// returns the number of elements read in
int vec_read_file (FILE* fptr, double v[], int dim) {
for (int i=0;i<dim;i++) {
if (fscanf(fptr,"%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);
// read a vector from a file
// returns the number of elements read in
int vec_read_file (FILE* fptr, 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