-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
407 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <mpi.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#define LUMP 5 | ||
|
||
int main(int argc, char **argv) { | ||
|
||
int numP, procID; | ||
|
||
// the usual mpi initialization | ||
MPI_Init(&argc, &argv); | ||
MPI_Comm_size(MPI_COMM_WORLD, &numP); | ||
MPI_Comm_rank(MPI_COMM_WORLD, &procID); | ||
|
||
int *globalData=NULL; | ||
int localData[LUMP]; | ||
|
||
// process 0 is only 1 that needs global data | ||
if (procID == 0) { | ||
globalData = malloc(LUMP * numP * sizeof(int) ); | ||
for (int i=0; i<LUMP*numP; i++) | ||
globalData[i] = 0; | ||
} | ||
|
||
for (int i=0; i<LUMP; i++) | ||
localData[i] = procID*10+i; | ||
|
||
MPI_Gather(localData, LUMP, MPI_INT, globalData, LUMP, MPI_INT, 0, MPI_COMM_WORLD); | ||
|
||
if (procID == 0) { | ||
for (int i=0; i<numP*LUMP; i++) | ||
printf("%d ", globalData[i]); | ||
printf("\n"); | ||
} | ||
|
||
if (procID == 0) | ||
free(globalData); | ||
|
||
MPI_Finalize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
#include <time.h> | ||
#include <math.h> | ||
|
||
static long int numSteps = 1000000000; | ||
|
||
int main() { | ||
|
||
// perform calculation | ||
double pi = 0; | ||
double dx = 1./numSteps; | ||
double x = dx*0.50; | ||
|
||
for (int i=0; i<numSteps; i++) { | ||
pi += 4./(1.+x*x); | ||
x += dx; | ||
} | ||
|
||
pi *= dx; | ||
|
||
printf("PI = %16.14f Difference from math.h definition %16.14f \n",pi, pi-M_PI); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <mpi.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
#define LUMP 1 | ||
|
||
static long int numSteps = 1000000000; | ||
|
||
int main(int argc, char **argv) { | ||
|
||
int numP, procID; | ||
|
||
// the usual mpi initialization | ||
MPI_Init(&argc, &argv); | ||
MPI_Comm_size(MPI_COMM_WORLD, &numP); | ||
MPI_Comm_rank(MPI_COMM_WORLD, &procID); | ||
|
||
double *globalData=NULL; | ||
double localData[LUMP]; | ||
|
||
// process 0 is only 1 that needs global data | ||
if (procID == 0) { | ||
globalData = (double *)malloc(LUMP * numP * sizeof(double) ); | ||
for (int i=0; i<LUMP*numP; i++) | ||
globalData[i] = 0; | ||
} | ||
double myPart = 0; | ||
double dx = 1./numSteps; | ||
double x = dx*0.50; | ||
|
||
for (int i=procID; i<numSteps; i+=numP) { | ||
x = (i+.5)*dx; | ||
myPart += 4./(1.+x*x); | ||
} | ||
|
||
myPart *= dx; | ||
|
||
// localData[0] = myPart; | ||
|
||
MPI_Gather(&myPart, LUMP, MPI_DOUBLE, globalData, LUMP, MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
|
||
double pi = 0; | ||
if (procID == 0) { | ||
for (int i=0; i<numP; i++) | ||
pi += globalData[i]; | ||
printf("PI = %16.14f Difference from math.h definition %16.14f \n",pi, pi-M_PI); | ||
} | ||
|
||
|
||
if (procID == 0) | ||
free(globalData); | ||
|
||
MPI_Finalize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
#-------------------------------------------------------------------- | ||
# Generic SLURM script – MPI Hello World | ||
# | ||
# This script requests 1 node and 8 cores/node (out of total 64 avail) | ||
# for a total of 1*8 = 8 MPI tasks. | ||
#--------------------------------------------------------------------- | ||
#SBATCH -J myjob | ||
#SBATCH -o myjob.%j.out | ||
#SBATCH -e myjob.%j.err | ||
#SBATCH -p development | ||
#SBATCH -N 1 | ||
#SBATCH -n 8 | ||
#SBATCH -t 00:02:00 | ||
#SBATCH -A DesignSafe-SimCenter | ||
|
||
ibrun ./pi | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
#-------------------------------------------------------------------- | ||
# Generic SLURM script – MPI Hello World | ||
# | ||
# This script requests 1 node and 8 cores/node (out of total 64 avail) | ||
# for a total of 1*8 = 8 MPI tasks. | ||
#--------------------------------------------------------------------- | ||
#SBATCH -J myjob | ||
#SBATCH -o myjob.%j.out | ||
#SBATCH -e myjob.%j.err | ||
#SBATCH -p development | ||
#SBATCH -N 1 | ||
#SBATCH -n 1 | ||
#SBATCH -t 00:02:00 | ||
#SBATCH -A DesignSafe-SimCenter | ||
|
||
ibrun ./pi | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <mpi.h> | ||
|
||
int main(int argc, char** argv) { | ||
|
||
int procID, numP; | ||
|
||
double* globalVector = NULL; | ||
double* localVector = NULL; | ||
|
||
MPI_Init(&argc, &argv); | ||
MPI_Comm_rank(MPI_COMM_WORLD, &procID); | ||
MPI_Comm_size(MPI_COMM_WORLD, &numP); | ||
|
||
if (argc != 2) { | ||
printf("Error correct usage: app vectorSize\n"); | ||
return 0; | ||
} | ||
int vectorSize = atoi(argv[1]); | ||
int remainder = vectorSize % numP; | ||
|
||
// Only the root process initializes the global array | ||
if (procID == 0) { | ||
globalVector = (double*)malloc(sizeof(double) * vectorSize); | ||
srand(50); | ||
for (int i = 0; i < vectorSize; i++) { | ||
double random_number = 1.0 + (double)rand() / RAND_MAX; | ||
globalVector[i] = random_number; | ||
} | ||
} | ||
|
||
// Determine the size of the local array for each process | ||
int localSize = vectorSize / numP; | ||
|
||
// Allocate memory for the local array | ||
localVector = (double*)malloc(sizeof(double) * localSize); | ||
|
||
// Scatter the global array to all processes | ||
MPI_Scatter(globalVector, localSize, MPI_DOUBLE, | ||
localVector, localSize, MPI_DOUBLE, | ||
0, MPI_COMM_WORLD); | ||
|
||
// Print the local array for each process | ||
printf("Process %d received: ", procID); | ||
for (int i = 0; i < localSize; i++) { | ||
printf("%.2f ", localVector[i]); | ||
} | ||
printf("\n"); | ||
|
||
// process0 has some stuff in the globalArray that was not sent! | ||
if (procID == 0) { | ||
printf("Process 0 Additional NOT SENT still in globalVector: "); | ||
for (int i=numP*localSize; i<vectorSize; i++) | ||
printf("%.2f ", globalVector[i]); | ||
printf("\n"); | ||
} | ||
|
||
// Clean up memory | ||
free(globalVector); | ||
free(localVector); | ||
|
||
MPI_Finalize(); | ||
return 0; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.