Logo AND Algorithmique Numérique Distribuée

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