Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't tell 165 times that we need CMAKE v2.6
[simgrid.git] / teshsuite / smpi / mpich3-test / perf / transp-datatype.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2006 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /*    modified 01/23/2011 by Jim Hoekstra - ISU
7  *      changed test to follow mtest_init/mtest_finalize convention
8  *      The following changes are based on suggestions from Chris Sadlo:
9  *        variable row changed to col.
10  *        manual transpose - code added to perform 'swap'.
11  *        MPI_Send/MPI_Recv involving xpose changed.
12  */
13
14 /* This is based on an example in the MPI standard and a bug report submitted
15    by Alexandr Konovalov of Intel */
16
17 #include "mpi.h"
18 #include <stdio.h>
19 #include "mpitest.h"
20
21 #define SIZE 100
22 #define ITER 100
23
24 int main(int argc, char* argv[])
25 {
26     int i, j, k;
27     static double a[SIZE][SIZE],b[SIZE][SIZE];
28     double t1,t2,t,ts,tst;
29     double temp;
30     int myrank, mysize, errs = 0;
31     MPI_Status status;
32     MPI_Aint sizeofreal;
33
34     MPI_Datatype col, xpose;
35
36     MTest_Init( &argc, &argv );
37     MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
38     MPI_Comm_size( MPI_COMM_WORLD, &mysize );
39     if (mysize != 2) {
40         fprintf( stderr, "This test must be run with 2 processes\n" );
41         MPI_Abort( MPI_COMM_WORLD, 1 );
42     }
43
44     MPI_Type_extent(MPI_DOUBLE, &sizeofreal);
45  
46     MPI_Type_vector(SIZE, 1, SIZE, MPI_DOUBLE, &col);
47     MPI_Type_hvector(SIZE, 1, sizeofreal, col, &xpose);
48     MPI_Type_commit(&xpose);
49
50     /* Preset the arrays so that they're in memory */
51     for (i=0; i<SIZE; i++)
52         for (j=0; j<SIZE; j++) {
53             a[i][j]=0;
54             b[i][j]=0;
55             }
56     a[SIZE-1][0] = 1;
57
58     /* Time the transpose example */
59     MPI_Barrier(MPI_COMM_WORLD);
60     t1=MPI_Wtime();
61     for(i=0;i< ITER; i++)
62         {
63             if(myrank==0)
64                 MPI_Send(&a[0][0],SIZE*SIZE,MPI_DOUBLE,1,0,MPI_COMM_WORLD);
65             else 
66                 MPI_Recv(&b[0][0],1,xpose,0,0,MPI_COMM_WORLD,&status);
67         }
68     t2=MPI_Wtime();
69     t=(t2-t1)/ITER;
70
71     /* Time sending the same amount of data, but without the transpose */
72     MPI_Barrier(MPI_COMM_WORLD);
73     t1=MPI_Wtime();
74     for(i=0; i< ITER; i++){
75         if(myrank==0)
76             {
77                 MPI_Send(&a[0][0],sizeof(a),MPI_BYTE,1,0,MPI_COMM_WORLD);
78             }
79         else {
80                 MPI_Recv(&b[0][0],sizeof(b),MPI_BYTE,0,0,MPI_COMM_WORLD,&status);
81         }
82     }
83     t2=MPI_Wtime();
84     ts=(t2-t1)/ITER;
85
86     /* Time sending the same amount of data, with the transpose done
87        as a separate step */
88     MPI_Barrier(MPI_COMM_WORLD);
89     t1=MPI_Wtime();
90     for(k=0; k< ITER; k++){
91         if(myrank==0)
92             {
93                 MPI_Send(&a[0][0],sizeof(a),MPI_BYTE,1,0,MPI_COMM_WORLD);
94             }
95         else {
96                 MPI_Recv(&b[0][0],sizeof(b),MPI_BYTE,0,0,MPI_COMM_WORLD,&status);
97                 for(i=0;i<SIZE;i++)
98                     for(j=i;j<SIZE;j++) {
99                         temp=b[j][i];
100                         b[j][i]=b[i][j];
101                         b[i][j]=temp;
102                 }
103         }
104     }
105     t2=MPI_Wtime();
106     tst=(t2-t1)/ITER;
107
108     /* Print out the results */
109     if (myrank == 1) {
110         /* if t and tst are too different, then there is a performance
111            problem in the handling of the datatypes */
112         
113         if (t > 2 * tst) {
114             errs ++;
115             fprintf( stderr, "Transpose time with datatypes is more than twice time without datatypes\n" );
116             fprintf( stderr, "%f\t%f\t%f\n", t, ts, tst );
117         }
118     }
119
120     MPI_Type_free(&col);
121     MPI_Type_free(&xpose);
122
123     MTest_Finalize( errs );
124     MPI_Finalize();
125     return 0;
126 }