#include <iostream>
#include <mpi.h>
#include <pthread.h>
void* myApp(void* x) {
int* k = ((int*)x);
switch (k[0]) {
case 0:
pthread_exit((void*)(k[1] + k[2]));
break;
case 1:
pthread_exit((void*)(k[1] * k[2]));
break;
case 2:
pthread_exit((void*)(k[1]-k[2]));
break;
}
return NULL;
}
int main(int argc, char* argv[]) {
#define N 9
int array[N];
int i;
int id;
int ierr;
int master = 0;
int p;
static int r8_pi = 3;
MPI_Status status;
int Rt, R;
int S = 0;
ierr = MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
if (id == 0) {
for (i = 0; i < N; i++) {
array[i] = i * r8_pi;
}
}
MPI_Bcast(array, N, MPI_INT, master, MPI_COMM_WORLD);
int x[3];
pthread_t th;
for (int i = id; i < N - 2; i += 3) {
x[0] = i % 3;
if (i % 3 != 0)
x[i % 3] = array[i];
if ((i + 1) % 3 != 0)
x[(i + 1) % 3] = array[i + 1];
if ((i + 2) % 3 != 0)
x[(i + 2) % 3] = array[i + 2];
pthread_create(&th, NULL, myApp, (void *)x);
pthread_join(th, (void **)&Rt);
S += Rt;
}
if (id != 0) {
printf("P= %d, S= %d\n", id
, S
); MPI_Send(&S, 1, MPI_INT, master, 1, MPI_COMM_WORLD);
} else {
R = S;
printf("P= %d, S= %d\n", id
, S
); for (i = 1; i < p; i++) {
MPI_Recv(&S, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status);
R += S;
}
}
MPI_Finalize();
return 0;
#undef N
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bXBpLmg+CiNpbmNsdWRlIDxwdGhyZWFkLmg+Cgp2b2lkKiBteUFwcCh2b2lkKiB4KSB7CiAgICBpbnQqIGsgPSAoKGludCopeCk7CiAgICBzd2l0Y2ggKGtbMF0pIHsKICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgIHB0aHJlYWRfZXhpdCgodm9pZCopKGtbMV0gKyBrWzJdKSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgMToKICAgICAgICAgICAgcHRocmVhZF9leGl0KCh2b2lkKikoa1sxXSAqIGtbMl0pKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSAyOgogICAgICAgICAgICBwdGhyZWFkX2V4aXQoKHZvaWQqKShrWzFdLWtbMl0pKTsKICAgICAgICAgICAgYnJlYWs7CiAgICB9CiAgICByZXR1cm4gTlVMTDsKfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqIGFyZ3ZbXSkgewogICAgI2RlZmluZSBOIDkKCiAgICBpbnQgYXJyYXlbTl07CiAgICBpbnQgaTsKICAgIGludCBpZDsKICAgIGludCBpZXJyOwogICAgaW50IG1hc3RlciA9IDA7CiAgICBpbnQgcDsKICAgIHN0YXRpYyBpbnQgcjhfcGkgPSAzOwogICAgTVBJX1N0YXR1cyBzdGF0dXM7CiAgICBpbnQgUnQsIFI7CiAgICBpbnQgUyA9IDA7CgogICAgaWVyciA9IE1QSV9Jbml0KCZhcmdjLCAmYXJndik7CiAgICBNUElfQ29tbV9zaXplKE1QSV9DT01NX1dPUkxELCAmcCk7CiAgICBNUElfQ29tbV9yYW5rKE1QSV9DT01NX1dPUkxELCAmaWQpOwoKICAgIGlmIChpZCA9PSAwKSB7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgICAgICBhcnJheVtpXSA9IGkgKiByOF9waTsKICAgICAgICB9CiAgICB9CgogICAgTVBJX0JjYXN0KGFycmF5LCBOLCBNUElfSU5ULCBtYXN0ZXIsIE1QSV9DT01NX1dPUkxEKTsKICAgIGludCB4WzNdOwogICAgcHRocmVhZF90IHRoOwoKICAgIGZvciAoaW50IGkgPSBpZDsgaSA8IE4gLSAyOyBpICs9IDMpIHsKICAgICAgICB4WzBdID0gaSAlIDM7CiAgICAgICAgaWYgKGkgJSAzICE9IDApCiAgICAgICAgICAgIHhbaSAlIDNdID0gYXJyYXlbaV07CiAgICAgICAgaWYgKChpICsgMSkgJSAzICE9IDApCiAgICAgICAgICAgIHhbKGkgKyAxKSAlIDNdID0gYXJyYXlbaSArIDFdOwogICAgICAgIGlmICgoaSArIDIpICUgMyAhPSAwKQogICAgICAgICAgICB4WyhpICsgMikgJSAzXSA9IGFycmF5W2kgKyAyXTsKCiAgICAgICAgcHRocmVhZF9jcmVhdGUoJnRoLCBOVUxMLCBteUFwcCwgKHZvaWQgKil4KTsKICAgICAgICBwdGhyZWFkX2pvaW4odGgsICh2b2lkICoqKSZSdCk7CiAgICAgICAgUyArPSBSdDsKICAgIH0KCiAgICBpZiAoaWQgIT0gMCkgewogICAgICAgIHByaW50ZigiUD0gJWQsIFM9ICVkXG4iLCBpZCwgUyk7CiAgICAgICAgTVBJX1NlbmQoJlMsIDEsIE1QSV9JTlQsIG1hc3RlciwgMSwgTVBJX0NPTU1fV09STEQpOwogICAgfSBlbHNlIHsKICAgICAgICBSID0gUzsKICAgICAgICBwcmludGYoIlA9ICVkLCBTPSAlZFxuIiwgaWQsIFMpOwogICAgICAgIGZvciAoaSA9IDE7IGkgPCBwOyBpKyspIHsKICAgICAgICAgICAgTVBJX1JlY3YoJlMsIDEsIE1QSV9JTlQsIE1QSV9BTllfU09VUkNFLCAxLCBNUElfQ09NTV9XT1JMRCwgJnN0YXR1cyk7CiAgICAgICAgICAgIFIgKz0gUzsKICAgICAgICB9CiAgICB9CgogICAgc3lzdGVtKCJwYXVzZSIpOwogICAgTVBJX0ZpbmFsaXplKCk7CiAgICByZXR1cm4gMDsKCiAgICAjdW5kZWYgTgp9Cgo=