source: CIVL/examples/mpi/collective/c_ex07.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[b28f397]1#include <stdio.h>
2#include <stdlib.h>
3#include <mpi.h>
4
5/*
6! This program shows how to use MPI_Alltoall. Each processor
7! send/rec a different random number to/from other processors.
8*/
9/* globals */
10int numnodes,myid,mpi_err;
11#define mpi_root 0
12/* end module */
13
14void init_it(int *argc, char ***argv);
15void seed_random(int id);
16void random_number(float *z);
17
18void init_it(int *argc, char ***argv) {
[06af073]19 mpi_err = MPI_Init(argc,argv);
[b28f397]20 mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &numnodes );
21 mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
22}
23
24int main(int argc,char *argv[]){
25 int *sray,*rray;
26 int *sdisp,*scounts,*rdisp,*rcounts;
27 int ssize,rsize,i,k,j;
28 float z;
29
30 init_it(&argc,&argv);
31 scounts=(int*)malloc(sizeof(int)*numnodes);
32 rcounts=(int*)malloc(sizeof(int)*numnodes);
33 sdisp=(int*)malloc(sizeof(int)*numnodes);
34 rdisp=(int*)malloc(sizeof(int)*numnodes);
35/*
36! seed the random number generator with a
37! different number on each processor
38*/
39 seed_random(myid);
40/* find data to send */
41 for(i=0;i<numnodes;i++){
42 random_number(&z);
43 scounts[i]=(int)(10.0*z)+1;
44 }
45 printf("myid= %d scounts=",myid);
46 for(i=0;i<numnodes;i++)
47 printf("%d ",scounts[i]);
48 printf("\n");
49/* send the data */
50 mpi_err = MPI_Alltoall( scounts,1,MPI_INT,
51 rcounts,1,MPI_INT,
52 MPI_COMM_WORLD);
53 printf("myid= %d rcounts=",myid);
54 for(i=0;i<numnodes;i++)
55 printf("%d ",rcounts[i]);
56 printf("\n");
[4358486]57#ifdef _CIVL
58free(scounts);
59free(rcounts);
60free(sdisp);
61free(rdisp);
62#endif
[b28f397]63 mpi_err = MPI_Finalize();
64}
65
66void seed_random(int id){
67 srand((unsigned int)id);
68}
69void random_number(float *z){
70 int i;
71 i=rand();
72 *z=(float)i/RAND_MAX;
73}
Note: See TracBrowser for help on using the repository browser.