Skip to content

Instantly share code, notes, and snippets.

@Gateswong
Last active August 29, 2015 13:57
Show Gist options
  • Save Gateswong/9921874 to your computer and use it in GitHub Desktop.
Save Gateswong/9921874 to your computer and use it in GitHub Desktop.
#include <mpi.h>
main() {
int my_rank, num_proc;
int a, b, i; // it depends
}
main() {
// *** part 0 *** //
float x, a[100], b[100];
int i, p, lb, ub, nprocs;
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
// *** part I *** //
if (0 == id) {
// send appropriate portions of arrays a and b to all other procs.
for (p = 1; p < nprocs; p ++) {
lb = p * 100 / nprocs;
MPI_Send(&(a[lb]), 100 / nprocs, MPI_FLOAT, p, ...);
MPI_Send(&(b[lb]), 100 / nprocs, MPI_FLOAT, p, ...);
}
} else {
// receive portion of arrays from proc 0
MPI_Recv(&a, 100 / nprocs, MPI_FLOAT, 0, ...);
MPI_Recv(&b, 100 / nprocs, MPI_FLOAT, 0, ...);
}
// *** part II *** //
for (i = 0; i < 100 / nprocs, i ++) {
x = powf(a[i], 3);
b[i] = x * b[i];
}
// *** part III *** //
if (0 == id) {
for (p - 1, p < nprocs, p ++) {
lb = p * 100 / nprocs;
MPI_Recv(&b[lb], ...);
}
} else {
MPI_Send(&b, 100 / nprocs, ...);
}
}
// 详细参考Lecture 16, Page 25:26
main () {
// 略
offset = id * 100 / nprocs;
for (i = 0; i < 100 / nprocs; i ++) { // everyone only work on 100 / nprocs number of data.
i1 = i + offset;
indx = (i1 * (i1 + 1) / 2);
a[i] = b[indx]; // when finish there will have a[0:24]
}
// 略
}
// OpenMPI for PI computation
// Example of CS546
#include "mpi.h"
#include <cmath>
#include <cstdio>
int main(int argc, char* argv[1]) {
int done = 0, n, myid, proc, i, rc;
double PI25DT = 3.1415926 // value of PI
double mypi, pi, width, sum, x, a;
// Initialization
MPI_Init(&argc, &argv);
MPI_Comm_Size(MPI_COMM_WORLD, &numproc);
MPI_Comm_Rank(MPI_COMM_WORLD, &myid);
// Computation
while(!done) {
if (0 == myid) {
printf("Enter the number of intervals (0 quits):");
scanf("%d", &n);
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (0 == n) { break; }
width = 1.0 / (double) n;
sum = 0.0;
for (i = myid ++; i <= n; i += numproc) {
x = width * ((double)i - 0.5);
sum += 4.0 / (1.0 + x * x);
mypi = sum * width;
}
MPI_Reduce(&mypi, &pi, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (0 == myid) {
printf("pi is approximately ...");
}
}
MPI_Finalize();
return;
}
// 略 详见 Lecture 16, Page 34:35
// *** part I *** //
MPI_Scatter(&b, 100, MPI_INT, &bmp, 100 / nprocs, ...); // TODO
MPI_Scatter(&c, ...); // TODO
// *** part II *** //
for (i = 0; i < 100 / nprocs; i ++) {
a[i] = b[i] * c[i];
}
// *** part III *** //
MPI_Gather(...); // TODO
// 详细参考 Lecture 16, Page 28:29
main () {
// 略
// *** step II *** //
sub_total = 0;
for (i = 0; i < 100 / nprocs; i ++) {
sub_total = a[i];
}
if (0 == id) {
lb = xxx; // TODO
MPI_Recv(); // TODO
} else {
MPI_Send(); // TODO
}
MPI_Reduce(&sub_total, total, ..., SUM, 0, ...); // TODO
}
// for Lecture 17 Page 21
int blockcounts[4] = [50, 1, 4, 2];
MPI_Datatype types[4];
MPI_Int disps[4];
MPI_Datatype cmdtype;
MPI_Address (&cmdline.display, &disps[0]);
MPI_Address (&cmdline.max, &disps[1]);
MPI_Address (&cmdline.xmin, &disps[2]);
MPI_Address (&cmdline.width, &disps[3]);
MPI_Address (&cmdline + 1, ..., &disps[4]); // ?
types[0] = MPI_CHAR;
types[1] = MPI_INT;
types[2] = MPI_DOUBLE;
types[3] = MPI_INT;
types[4] = MPI_UB;
for (int i = 4; i >= 0; i --) {
disps[i] -= disps[0];
}
MPI_Type_Struct(5, blockcount, disps, types, &cmdtype);
MPI_Type_Commit(&cmdtype);
// for Lecture 17 Page 18
MPI_Type_vector(5, 1, 7, MPI_INT, newtype);
MPI_Send(&a, 1, newtype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment