Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compare files.
[simgrid.git] / teshsuite / smpi / alltoallv_coll.c
1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include <string.h>
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "mpi.h"
11
12 /*
13    This program tests MPI_Alltoallv by having processor i send different
14    amounts of data to each processor.
15
16    Because there are separate send and receive types to alltoallv,
17    there need to be tests to rearrange data on the fly.  Not done yet.
18
19    The first test sends i items to processor i from all processors.
20
21    Currently, the test uses only MPI_INT; this is adequate for testing systems
22    that use point-to-point operations
23  */
24
25
26 /* example values:
27  * For 3 processes:
28  * <0> sbuf: (#9):   [0][1][2][3][4][5][6][7][8]
29    <0> scount: (#3): [0][1][2]
30    <0> rcount: (#3): [0][0][0]
31    <0> sdisp: (#3):  [0][1][3]
32    <0> rdisp: (#3):  [0][0][0]
33
34    <1> sbuf: (#9):   [100][101][102][103][104][105][106][107][108]
35    <1> scount: (#3): [0][1][2]
36    <1> rcount: (#3): [1][1][1]
37    <1> sdisp: (#3):  [0][1][3]
38    <1> rdisp: (#3):  [0][1][2]
39
40    <2> sbuf: (#9):   [200][201][202][203][204][205][206][207][208]
41    <2> scount: (#3): [0][1][2]
42    <2> rcount: (#3): [2][2][2]
43    <2> sdisp: (#3):  [0][1][3]
44    <2> rdisp: (#3):  [0][2][4]
45
46    after MPI_Alltoallv :
47    <0> rbuf: (#9):   [-1][-1][-1][-1][-1][-1][-1][-1][-1]
48    <1> rbuf: (#9):   [1][101][201][-1][-1][-1][-1][-1][-1]
49    <2> rbuf: (#9):   [3][4][103][104][203][204][-1][-1][-1]
50 */
51
52
53 static void print_buffer_int(void *buf, int len, char *msg, int rank)
54 {
55   int tmp, *v;
56   printf("[%d] %s (#%d): ", rank, msg, len);
57   for (tmp = 0; tmp < len; tmp++) {
58     v = buf;
59     printf("[%d]", v[tmp]);
60   }
61   printf("\n");
62   free(msg);
63 }
64
65
66 int main(int argc, char **argv)
67 {
68
69   MPI_Comm comm;
70   int *sbuf, *rbuf;
71   int rank, size;
72   int *sendcounts, *recvcounts, *rdispls, *sdispls;
73   int i;
74
75   MPI_Init(&argc, &argv);
76
77   comm = MPI_COMM_WORLD;
78
79   /* Create the buffer */
80   MPI_Comm_size(comm, &size);
81   MPI_Comm_rank(comm, &rank);
82   sbuf = (int *) xbt_malloc(size * size * sizeof(int));
83   rbuf = (int *) xbt_malloc(size * size * sizeof(int));
84
85   /* Load up the buffers */
86   for (i = 0; i < size * size; i++) {
87     sbuf[i] = i + 100 * rank;
88     rbuf[i] = -1;
89   }
90
91   /* Create and load the arguments to alltoallv */
92   sendcounts = (int *) xbt_malloc(size * sizeof(int));
93   recvcounts = (int *) xbt_malloc(size * sizeof(int));
94   rdispls = (int *) xbt_malloc(size * sizeof(int));
95   sdispls = (int *) xbt_malloc(size * sizeof(int));
96   for (i = 0; i < size; i++) {
97     sendcounts[i] = i;
98     recvcounts[i] = rank;
99     rdispls[i] = i * rank;
100     sdispls[i] = (i * (i + 1)) / 2;
101   }
102  
103   print_buffer_int( sbuf, size*size, strdup("sbuf:"),rank);
104   print_buffer_int( sendcounts, size, strdup("scount:"),rank);
105   print_buffer_int( recvcounts, size, strdup("rcount:"),rank);
106   print_buffer_int( sdispls, size, strdup("sdisp:"),rank);
107   print_buffer_int( rdispls, size, strdup("rdisp:"),rank);
108    
109   MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT,
110                 rbuf, recvcounts, rdispls, MPI_INT, comm);
111
112   print_buffer_int( rbuf, size*size, strdup("rbuf:"),rank);
113
114   MPI_Barrier(MPI_COMM_WORLD);  
115   if (0 == rank) {
116     printf("Alltoallv TEST COMPLETE.\n");
117   }
118   free(sdispls);
119   free(rdispls);
120   free(recvcounts);
121   free(sendcounts);
122   free(rbuf);
123   free(sbuf);
124
125   MPI_Finalize();
126   return 0;
127 }