allred.c

来自「fortran并行计算包」· C语言 代码 · 共 1,918 行 · 第 1/5 页

C
1,918
字号
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* *  (C) 2001 by Argonne National Laboratory. *      See COPYRIGHT in top-level directory. */#include <math.h>#include "mpi.h"#include <stdio.h>#include <stdlib.h>/* Run this test on 2, 4, 6, 8, or 10 processes only */int count, errcnt = 0, gerr = 0, size, rank;MPI_Comm comm;void a1(void),  b1(void),  c1(void),  d1(void),  e1(void),  f1(void),  g1(void),  h1(void),  a2(void),  b2(void),  c2(void),  d2(void),  e2(void),  f2(void),  g2(void),  h2(void),  a3(void),  b3(void),  c3(void),  d3(void),  e3(void),  f3(void),  g3(void),  h3(void),  a4(void),  b4(void),  c4(void),  d4(void),  e4(void),  f4(void),  g4(void),  h4(void),  a5(void),  b5(void),  c5(void),  d5(void),  e5(void),  f5(void),  a6(void),  b6(void),  c6(void),  d6(void),  e6(void),  f6(void),  a7(void),  b7(void),  c7(void),  d7(void),  e7(void),  f7(void),  a8(void),  b8(void),  c8(void),  d8(void),  e8(void),  f8(void),  a9(void),  b9(void),  c9(void),  d9(void),  e9(void),  f9(void),  a10(void),  b10(void),  c10(void),  d10(void),  e10(void),  f10(void),  a11(void),  b11(void),  c11(void),  d11(void),  e11(void),  f11(void),  a12(void),  b12(void),  c12(void),  d12(void),  e12(void),  f12(void),  g12(void),  a13(void),  b13(void),  c13(void),  d13(void),  e13(void),  f13(void),  g13(void),  a14(void),  b14(void),  c14(void),  d14(void),  e14(void),  f14(void),  a15(void),  b15(void),  c15(void),  d15(void),  e15(void),  f15(void),  a16(void),  b16(void),  c16(void),  d16(void),  e16(void),  f16(void),  a17(void),  b17(void),  c17(void),  d17(void),  e17(void),  f17(void),  a18(void),  b18(void),  c18(void),  d18(void),  e18(void),  a19(void),  b19(void),  c19(void),  d19(void),  e19(void);void a1(void){    int *in, *out, *sol;    int  i, fnderr=0;    in = (int *)malloc( count * sizeof(int) );    out = (int *)malloc( count * sizeof(int) );    sol = (int *)malloc( count * sizeof(int) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_INT, MPI_SUM, MPI_COMM_WORLD );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_INT and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void b1(void){    long *in, *out, *sol;    int  i, fnderr=0;    in = (long *)malloc( count * sizeof(long) );    out = (long *)malloc( count * sizeof(long) );    sol = (long *)malloc( count * sizeof(long) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_LONG, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_LONG and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void c1(void){    short *in, *out, *sol;    int  i, fnderr=0;    in = (short *)malloc( count * sizeof(short) );    out = (short *)malloc( count * sizeof(short) );    sol = (short *)malloc( count * sizeof(short) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_SHORT, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_SHORT and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void d1(void){    unsigned short *in, *out, *sol;    int  i, fnderr=0;    in = (unsigned short *)malloc( count * sizeof(unsigned short) );    out = (unsigned short *)malloc( count * sizeof(unsigned short) );    sol = (unsigned short *)malloc( count * sizeof(unsigned short) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_UNSIGNED_SHORT, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_UNSIGNED_SHORT and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void e1(void){    unsigned *in, *out, *sol;    int  i, fnderr=0;    in = (unsigned *)malloc( count * sizeof(unsigned) );    out = (unsigned *)malloc( count * sizeof(unsigned) );    sol = (unsigned *)malloc( count * sizeof(unsigned) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_UNSIGNED, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_UNSIGNED and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void f1(void){    unsigned long *in, *out, *sol;    int  i, fnderr=0;    in = (unsigned long *)malloc( count * sizeof(unsigned long) );    out = (unsigned long *)malloc( count * sizeof(unsigned long) );    sol = (unsigned long *)malloc( count * sizeof(unsigned long) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_UNSIGNED_LONG, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_UNSIGNED_LONG and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void g1(void){    float *in, *out, *sol;    int  i, fnderr=0;    in = (float *)malloc( count * sizeof(float) );    out = (float *)malloc( count * sizeof(float) );    sol = (float *)malloc( count * sizeof(float) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_FLOAT, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_FLOAT and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void h1(void){    double *in, *out, *sol;    int  i, fnderr=0;    in = (double *)malloc( count * sizeof(double) );    out = (double *)malloc( count * sizeof(double) );    sol = (double *)malloc( count * sizeof(double) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = i*size;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_DOUBLE, MPI_SUM, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_DOUBLE and op MPI_SUM\n", rank );    free( in );    free( out );    free( sol );}void a2(void){    int *in, *out, *sol;    int  i, fnderr=0;    in = (int *)malloc( count * sizeof(int) );    out = (int *)malloc( count * sizeof(int) );    sol = (int *)malloc( count * sizeof(int) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_INT, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_INT and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void b2(void){    long *in, *out, *sol;    int  i, fnderr=0;    in = (long *)malloc( count * sizeof(long) );    out = (long *)malloc( count * sizeof(long) );    sol = (long *)malloc( count * sizeof(long) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_LONG, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_LONG and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void c2(void){    short *in, *out, *sol;    int  i, fnderr=0;    in = (short *)malloc( count * sizeof(short) );    out = (short *)malloc( count * sizeof(short) );    sol = (short *)malloc( count * sizeof(short) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_SHORT, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_SHORT and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void d2(void){    unsigned short *in, *out, *sol;    int  i, fnderr=0;    in = (unsigned short *)malloc( count * sizeof(unsigned short) );    out = (unsigned short *)malloc( count * sizeof(unsigned short) );    sol = (unsigned short *)malloc( count * sizeof(unsigned short) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_UNSIGNED_SHORT, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_UNSIGNED_SHORT and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void e2(void){    unsigned *in, *out, *sol;    int  i, fnderr=0;    in = (unsigned *)malloc( count * sizeof(unsigned) );    out = (unsigned *)malloc( count * sizeof(unsigned) );    sol = (unsigned *)malloc( count * sizeof(unsigned) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_UNSIGNED, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_UNSIGNED and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void f2(void){    unsigned long *in, *out, *sol;    int  i, fnderr=0;    in = (unsigned long *)malloc( count * sizeof(unsigned long) );    out = (unsigned long *)malloc( count * sizeof(unsigned long) );    sol = (unsigned long *)malloc( count * sizeof(unsigned long) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_UNSIGNED_LONG, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_UNSIGNED_LONG and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void g2(void){    float *in, *out, *sol;    int  i, fnderr=0;    in = (float *)malloc( count * sizeof(float) );    out = (float *)malloc( count * sizeof(float) );    sol = (float *)malloc( count * sizeof(float) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_FLOAT, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_FLOAT and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void h2(void){    double *in, *out, *sol;    int  i, fnderr=0;    in = (double *)malloc( count * sizeof(double) );    out = (double *)malloc( count * sizeof(double) );    sol = (double *)malloc( count * sizeof(double) );    for (i=0; i<count; i++) { *(in + i) = i; *(sol + i) = (i > 0) ? (int)(pow((double)(i),(double)size)+0.1) : 0;     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_DOUBLE, MPI_PROD, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_DOUBLE and op MPI_PROD\n", rank );    free( in );    free( out );    free( sol );}void a3(void){    int *in, *out, *sol;    int  i, fnderr=0;    in = (int *)malloc( count * sizeof(int) );    out = (int *)malloc( count * sizeof(int) );    sol = (int *)malloc( count * sizeof(int) );    for (i=0; i<count; i++) { *(in + i) = (rank + i); *(sol + i) = (size - 1 + i);     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_INT, MPI_MAX, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_INT and op MPI_MAX\n", rank );    free( in );    free( out );    free( sol );}void b3(void){    long *in, *out, *sol;    int  i, fnderr=0;    in = (long *)malloc( count * sizeof(long) );    out = (long *)malloc( count * sizeof(long) );    sol = (long *)malloc( count * sizeof(long) );    for (i=0; i<count; i++) { *(in + i) = (rank + i); *(sol + i) = (size - 1 + i);     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_LONG, MPI_MAX, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_LONG and op MPI_MAX\n", rank );    free( in );    free( out );    free( sol );}void c3(void){    short *in, *out, *sol;    int  i, fnderr=0;    in = (short *)malloc( count * sizeof(short) );    out = (short *)malloc( count * sizeof(short) );    sol = (short *)malloc( count * sizeof(short) );    for (i=0; i<count; i++) { *(in + i) = (rank + i); *(sol + i) = (size - 1 + i);     *(out + i) = 0; }    MPI_Allreduce( in, out, count, MPI_SHORT, MPI_MAX, comm );    for (i=0; i<count; i++) { if (*(out + i) != *(sol + i)) {errcnt++; fnderr++;}}    if (fnderr) fprintf( stderr, 	"(%d) Error for type MPI_SHORT and op MPI_MAX\n", rank );    free( in );    free( out );    free( sol );}void d3(void){    unsigned short *in, *out, *sol;    int  i, fnderr=0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?