source: CIVL/mods/dev.civl.abc/examples/mpi/adder_par.c

main
Last change on this file was aad342c, checked in by Stephen Siegel <siegel@…>, 3 years ago

Performing huge refactor to incorporate ABC, GMC, and SARL into CIVL repo and use Java modules.

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

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/* FEVS: A Functional Equivalence Verification Suite for High-Performance
2 * Scientific Computing
3 *
4 * Copyright (C) 2010, Stephen F. Siegel, Timothy K. Zirkel,
5 * University of Delaware
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA.
21 */
22
23#include<civlc.cvh>
24#include<stdio.h>
25#include<stdlib.h>
26#include"mpi.h"
27
28#pragma CIVL $input int NB;
29#pragma CIVL $output int __sum;
30double sum;
31double localSum = 0.0;
32int PID;
33int NPROCS;
34
35double computeGlobalSum() {
36 double result = localSum;
37 double buffer;
38 int i;
39 MPI_Status status;
40
41 for (i=1; i<NPROCS; i++) {
42 MPI_Recv(&buffer, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &status);
43 result += buffer;
44 }
45 return result;
46}
47
48int main(int argc, char *argv[]) {
49 int n = atoi(argv[1]);
50 #pragma CIVL $assume(0 < n && n <= NB);
51 MPI_Init(&argc, &argv);
52 MPI_Comm_size(MPI_COMM_WORLD, &NPROCS);
53 MPI_Comm_rank(MPI_COMM_WORLD, &PID);
54 int first = n*PID/NPROCS;
55 int afterLast = n*(PID+1)/NPROCS;
56 int i;
57 double a[n];
58 FILE *fp = fopen("data","r");
59
60 for (i=0; i<n; i++) fscanf(fp, "%lf", &a[i]);
61 for (i=first; i<afterLast; i++) localSum += a[i];
62 if (PID == 0) {
63 sum = computeGlobalSum();
64 printf("%lf", sum);
65 #pragma CIVL __sum = sum;
66 } else {
67 MPI_Send(&localSum, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
68 }
69 fclose(fp);
70 MPI_Finalize();
71 return 0;
72}
Note: See TracBrowser for help on using the repository browser.