1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>
int main(int argc, char **argv)
{
double *buf1, *buf2, *buf3, *buf4;
double *pack, *unpack;
double size;
double start, end;
int pos;
int i;
int rank;
int cnt;
int ierr;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
cnt = 100000;
size = 10;
buf1 = calloc(size, sizeof *buf1);
buf2 = calloc(size, sizeof *buf2);
buf3 = calloc(size, sizeof *buf3);
buf4 = calloc(size, sizeof *buf4);
pack = calloc(3*size, sizeof *pack);
unpack = calloc(3*size, sizeof *unpack);
for (i=0; i<size; i++)
{
buf1[i] = 2;
buf2[i] = 4;
}
MPI_Barrier(MPI_COMM_WORLD);
start = MPI_Wtime();
for (i=0; i< cnt; i++)
{
MPI_Reduce(buf1, // send buffer
buf3, // reduced data goes here
size, // size of the buffers
MPI_DOUBLE, // type of the data
MPI_SUM, // operation
0, // proc 0 only will recieve data
MPI_COMM_WORLD); // all procs do the reduction
MPI_Reduce(buf2, // send buffer
buf4, // reduced data goes here
size, // size of the buffers
MPI_DOUBLE, // type of the data
MPI_SUM, // operation
0, // proc 0 only will recieve data
MPI_COMM_WORLD); // all procs do the reduction
}
end = MPI_Wtime();
if(rank==0)
{
printf("%f seconds\n", end-start);
}
// empty buf3 and buf4
memset(buf3, 0, size);
memset(buf4, 0, size);
MPI_Barrier(MPI_COMM_WORLD);
start = MPI_Wtime();
for(i=0; i < cnt; i++)
{
pos = 0; // start to pack at pack+0
ierr = MPI_Pack(buf1, // data to be packed
size, // size of buf1
MPI_DOUBLE, // type of these elements
pack, // start adress of the pack
2*size*sizeof *pack, // size of the pack buffer
&pos, // pack+pos is the start position
MPI_COMM_WORLD); // every proc will use 'pack'
// pack the second buffer
MPI_Pack(buf2, // data to be packed
size, // size of buf1
MPI_DOUBLE, // type of these elements
pack, // start adress of the pack
2*size*sizeof *pack, // size of the pack buffer
&pos, // pack+pos is the start position
MPI_COMM_WORLD); // every proc will use 'pack'
// now reduce the pack
MPI_Reduce(pack, // send buffer
unpack, // reduced data goes here
2*size, // size of the buffers
MPI_DOUBLE, // type of the data
MPI_SUM, // operation
0, // proc 0 only will recieve data
MPI_COMM_WORLD); // all procs do the reduction
}
end = MPI_Wtime();
if(rank==0)
{
printf("%f seconds\n", end-start);
}
if (rank == 0)
{
for (i=0; i<2*size; i++)
{
printf("unpack[%d] = %f\n",i,unpack[i]);
}
}
free(buf1);
free(buf2);
free(buf3);
free(buf4);
free(pack);
free(unpack);
MPI_Finalize();
return 0;
} |
Partager