📄 allred.c
字号:
#include <math.h>#include "mpi.h"#include <stdio.h>#include <stdlib.h>#include "test.h"#include "../pt2pt/gcomm.h"int verbose = 0;int main( int argc, char **argv ){int count, errcnt = 0, gerr = 0, toterr, size, rank;MPI_Comm comm;MPI_Comm comms[10];int ncomm, ii, world_rank;MPI_Init( &argc, &argv );MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );/* First tests */MakeComms( comms, 10, &ncomm, 0 );for (ii=0; ii<ncomm; ii++) {if (world_rank == 0 && verbose) printf( "Testing with communicator %d\n", ii );comm = comms[ii];MPI_Comm_size( comm, &size );MPI_Comm_rank( comm, &rank );count = 10;/* Test sum */if (world_rank == 0 && verbose) printf( "Testing MPI_SUM...\n" );{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, 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_SUM\n", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}gerr += errcnt;if (errcnt > 0) printf( "Found %d errors on %d for MPI_SUM\n", errcnt, rank );errcnt = 0;/* Test product */if (world_rank == 0 && verbose) printf( "Testing MPI_PROD...\n" );{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}gerr += errcnt;if (errcnt > 0) printf( "Found %d errors on %d for MPI_PROD\n", errcnt, rank );errcnt = 0;/* Test max */if (world_rank == 0 && verbose) printf( "Testing MPI_MAX...\n" );{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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", world_rank );free( in );free( out );free( sol );}{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) = (rank + i); *(sol + i) = (size - 1 + i); *(out + i) = 0; }MPI_Allreduce( in, out, count, MPI_UNSIGNED_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_UNSIGNED_SHORT and op MPI_MAX\n", world_rank );free( in );free( out );free( sol );}{unsigned *in, *out, *sol;int i, fnderr=0;in = (unsigned *)malloc( count * sizeof(unsigned) );out = (unsigned *)malloc( count * sizeof(unsigned) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -