Skip to content

Instantly share code, notes, and snippets.

@maxisoft
Created January 25, 2016 10:27
Show Gist options
  • Save maxisoft/3669c206bed8a1d069e9 to your computer and use it in GitHub Desktop.
Save maxisoft/3669c206bed8a1d069e9 to your computer and use it in GitHub Desktop.
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
// macro pour tester le cas de tableaux non egaux.
//mettre a 0 pour que les 2 tableaux soit egaux. 1 sinon.
#define ARRAY_NOT_EQUAL 0
#define VECT_LEN 6
#define MASTER 0
int main (int argc, char *argv[])
{
int numtasks, /* number of tasks in partition */
taskid; /* a task identifier */
MPI_Status status;
int v1[VECT_LEN];
int v2[VECT_LEN];
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
if (VECT_LEN % numtasks)
{
printf("Mauvais rapport taille / nombre de processus !\n");
MPI_Abort(MPI_COMM_WORLD, 0);
exit(1);
}
int *vect = NULL;
const size_t vbuff_len = VECT_LEN * 2 / numtasks;
int vbuff[vbuff_len];
/* hold task results */
int vtmp[numtasks];
if (taskid == MASTER)
{
//construction des tableaux
for (int i = 0; i < VECT_LEN; ++i)
{
v1[i] = i + 1;
v2[i] = i + 1 + ARRAY_NOT_EQUAL;
}
//init vect
vect = calloc(VECT_LEN * 2, sizeof(int));
for (int i = 0, j = 0; i < VECT_LEN; ++i)
{
vect[j++] = v1[i];
vect[j++] = v2[i];
}
}
//envoi / repartition de vect a tous les noeuds
MPI_Scatter(
vect,
vbuff_len,
MPI_INT,
vtmp,
vbuff_len,
MPI_INT,
MASTER,
MPI_COMM_WORLD);
//calcul d'un resultat local a l'aide de vtmp
int equal = 1;
for (int i = 0; i < vbuff_len && equal; i += 2)
{
if(vtmp[i] != vtmp[i + 1])
{
equal = 0;
}
}
//envoi des resultats au processus maitre.
MPI_Gather(&equal, 1, MPI_INT, vtmp, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (taskid == MASTER)
{
for (int i = 0; i < numtasks && equal; ++i)
{
equal &= vtmp[i];
}
printf("arrays're %s equal\n", equal ? "" : "not");
}
if (vect != NULL)
{
free(vect);
vect = NULL;
}
MPI_Finalize();
}
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
// macro pour tester le cas de tableaux non egaux.
//mettre a 0 pour que les 2 tableaux soit egaux. 1 sinon.
#define ARRAY_NOT_EQUAL 0
#define VECT_LEN 6
#define MASTER 0
int main (int argc, char *argv[])
{
int numtasks, /* number of tasks in partition */
taskid; /* a task identifier */
MPI_Status status;
int v1[VECT_LEN];
int v2[VECT_LEN];
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
if (VECT_LEN % numtasks)
{
printf("Mauvais rapport taille / nombre de processus !\n");
MPI_Abort(MPI_COMM_WORLD, 0);
exit(1);
}
/* hold task results */
int vtmp[numtasks];
if (taskid == MASTER)
{
for (int i = 0; i < VECT_LEN; ++i)
{
v1[i] = i + 1;
v2[i] = i + 1 + ARRAY_NOT_EQUAL;
}
}
MPI_Scatter(
v1,
VECT_LEN / numtasks,
MPI_INT,
v1,
VECT_LEN / numtasks,
MPI_INT,
MASTER,
MPI_COMM_WORLD);
MPI_Scatter(
v2,
VECT_LEN / numtasks,
MPI_INT,
v2,
VECT_LEN / numtasks,
MPI_INT,
MASTER,
MPI_COMM_WORLD);
int equal = 1;
for (int i = 0; i < VECT_LEN / numtasks && equal; ++i)
{
if(v1[i] != v2[i])
{
equal = 0;
}
}
MPI_Gather(&equal, 1, MPI_INT, vtmp, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (taskid == MASTER)
{
for (int i = 0; i < numtasks && equal; ++i)
{
equal &= vtmp[i];
}
printf("arrays're %s equal\n", equal ? "" : "not");
}
MPI_Finalize();
}
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#define VECT_LEN 6
#define NBELEM 2
#define MASTER 0
// macro pour tester le cas de tableaux non egaux.
//mettre a 0 pour que les 2 tableaux soit egaux. 1 sinon.
#define ARRAY_NOT_EQUAL 0
//tags
#define TASK_REQUEST 5
#define TASK_PUSH 6
#define TASK_RESULT 7
#define TASK_NO_MORE 8
int main (int argc, char *argv[])
{
int numtasks, /* number of tasks in partition */
taskid; /* a task identifier */
MPI_Status status;
int v1[VECT_LEN];
int v2[VECT_LEN];
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
if (VECT_LEN % numtasks)
{
printf("Mauvais rapport taille / nombre de processus !\n");
MPI_Abort(MPI_COMM_WORLD, 0);
exit(1);
}
int vtmp[numtasks];
int nbelem = NBELEM;
int equal = 1;
if (taskid == MASTER)
{
int currentIndex = 0;
for (int i = 0; i < VECT_LEN; ++i)
{
v1[i] = i + 1;
v2[i] = i + 1 + ARRAY_NOT_EQUAL;
}
while(equal && currentIndex < VECT_LEN - 1)
{
MPI_Recv(
&nbelem,
1,
MPI_INT,
MPI_ANY_SOURCE,
MPI_ANY_TAG,
MPI_COMM_WORLD,
&status);
if(status.MPI_TAG == TASK_REQUEST)
{
int newIndex = (currentIndex + nbelem);
if (newIndex >= VECT_LEN)
{
newIndex = VECT_LEN - 1;
}
//Note : on peut regrouper ces 2 mpi_send en 1 seul.
MPI_Send(
v1,
newIndex - currentIndex,
MPI_INT,
status.MPI_SOURCE,
TASK_PUSH,
MPI_COMM_WORLD);
MPI_Send(
v2,
newIndex - currentIndex,
MPI_INT,
status.MPI_SOURCE,
TASK_PUSH,
MPI_COMM_WORLD);
currentIndex = newIndex;
}
else if (status.MPI_TAG == TASK_RESULT)
{
equal &= nbelem;
}
else
{
printf("unknow message tag %d\n", status.MPI_TAG);
}
}
//broadcast that the program ended
for (int i = 0; i < numtasks; ++i)
{
if(i != MASTER)
{
MPI_Send(&equal, 1, MPI_INT, i, TASK_NO_MORE, MPI_COMM_WORLD);
}
}
printf("arrays're %s equal\n", equal ? "" : "not");
}
else // worker code
{
do {
MPI_Send(
&nbelem,
1,
MPI_INT,
MASTER,
TASK_REQUEST,
MPI_COMM_WORLD);
MPI_Recv(
v1,
nbelem,
MPI_INT,
MASTER,
MPI_ANY_TAG,
MPI_COMM_WORLD,
&status);
if (status.MPI_TAG == TASK_NO_MORE)
{
break;
}
int v1_count = -1;
MPI_Get_count(&status, MPI_INT, &v1_count);
MPI_Recv(
v2,
nbelem,
MPI_INT,
MASTER,
MPI_ANY_TAG,
MPI_COMM_WORLD,
&status);
int v2_count = -1;
MPI_Get_count(&status, MPI_INT, &v2_count);
equal = v1_count == v2_count;
for (int i = 0; i < v1_count && equal; ++i)
{
if(v1[i] != v2[i]) {
equal = 0;
}
}
MPI_Send(
&equal,
1,
MPI_INT,
MASTER,
TASK_RESULT,
MPI_COMM_WORLD);
} while(equal && status.MPI_TAG != TASK_NO_MORE);
}
MPI_Finalize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment