source: CIVL/examples/mpi/collective/c_ex05.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: 2.2 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <mpi.h>
4#ifdef _CIVL
5#include<civlc.cvh>
6#endif
7
8/*
9! This program shows how to use MPI_Scatter and MPI_Gather
10! Each processor gets different data from the root processor
11! by way of mpi_scatter. The data is summed and then sent back
12! to the root processor using MPI_Gather. The root processor
13! then prints the global sum.
14*/
15/* globals */
16int numnodes,myid,mpi_err;
17#define mpi_root 0
18#ifdef _CIVL
19$input int count=4;
20#endif
21/* end globals */
22
23void init_it(int *argc, char ***argv);
24
25void init_it(int *argc, char ***argv) {
26 mpi_err = MPI_Init(argc,argv);
27 mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &numnodes );
28 mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
29}
30
31int main(int argc,char *argv[]){
32 int *myray,*send_ray=NULL,*back_ray=NULL;
33#ifndef _CIVL
34 int count;
35#endif
36 int size,mysize,i,k,j,total;
37
38 init_it(&argc,&argv);
39/* each processor will get count elements from the root */
40#ifndef _CIVL
41 count=4;
42#endif
43 myray=(int*)malloc(count*sizeof(int));
44/* create the data to be sent on the root */
45 if(myid == mpi_root){
46 size=count*numnodes;
47 send_ray=(int*)malloc(size*sizeof(int));
48 back_ray=(int*)malloc(numnodes*sizeof(int));
49 for(i=0;i<size;i++)
50 send_ray[i]=i;
51 }
52/* send different data to each processor */
53 mpi_err = MPI_Scatter( send_ray, count, MPI_INT,
54 myray, count, MPI_INT,
55 mpi_root,
56 MPI_COMM_WORLD);
57
58/* each processor does a local sum */
59 total=0;
60 for(i=0;i<count;i++)
61 total=total+myray[i];
62 printf("myid= %d total= %d\n",myid,total);
63#ifdef _CIVL
64 $assert(total == count*(2*myid*count + count - 1)/2);
65#endif
66/* send the local sums back to the root */
67 mpi_err = MPI_Gather(&total, 1, MPI_INT,
68 back_ray, 1, MPI_INT,
69 mpi_root,
70 MPI_COMM_WORLD);
71/* the root prints the global sum */
72 if(myid == mpi_root){
73 total=0;
74 for(i=0;i<numnodes;i++)
75 total=total+back_ray[i];
76 printf("results from all processors= %d \n",total);
77#ifdef _CIVL
78 $assert(total == (size * size - size)/2);
79#endif
80 }
81#ifdef _CIVL
82 free(myray);
83 free(send_ray);
84 free(back_ray);
85#endif
86 mpi_err = MPI_Finalize();
87}
Note: See TracBrowser for help on using the repository browser.