📄 contents.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */#include "mpi.h"#include "mpitestconf.h"#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#include <stdio.h>#include <stdlib.h>static int verbose = 0;/* tests */int builtin_float_test(void);int vector_of_vectors_test(void);int optimizable_vector_of_basics_test(void);int indexed_of_basics_test(void);int indexed_of_vectors_test(void);int struct_of_basics_test(void);/* helper functions */char *combiner_to_string(int combiner);int parse_args(int argc, char **argv);int main(int argc, char **argv){ int err, errs = 0; MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */ parse_args(argc, argv); /* perform some tests */ err = builtin_float_test(); errs += err; if (err) { fprintf(stderr, "Found %d errors in builtin float test.\n", err); } err = vector_of_vectors_test(); errs += err; if (err) { fprintf(stderr, "Found %d errors in vector of vectors test.\n", err); } err = optimizable_vector_of_basics_test(); errs += err; if (err) { fprintf(stderr, "Found %d errors in vector of basics test.\n", err); } err = indexed_of_basics_test(); errs += err; if (err) { fprintf(stderr, "Found %d errors in indexed of basics test.\n", err); } err = indexed_of_vectors_test(); errs += err; if (err) { fprintf(stderr, "Found %d errors in indexed of vectors test.\n", err); }#ifdef HAVE_MPI_TYPE_CREATE_STRUCT err = struct_of_basics_test(); errs += err;#endif /* print message and exit */ if (errs) { fprintf(stderr, "Found %d errors\n", errs); } else { printf(" No Errors\n"); } MPI_Finalize(); return 0;}/* builtin_float_test() * * Tests functionality of get_envelope() and get_contents() on a MPI_FLOAT. * * Returns the number of errors encountered. */int builtin_float_test(void){ int nints, nadds, ntypes, combiner; int err, errs = 0; err = MPI_Type_get_envelope(MPI_FLOAT, &nints, &nadds, &ntypes, &combiner); if (combiner != MPI_COMBINER_NAMED) errs++; if (verbose && combiner != MPI_COMBINER_NAMED) fprintf(stderr, "combiner = %s; should be named\n", combiner_to_string(combiner)); /* Note: it is erroneous to call MPI_Type_get_contents() on a basic. */ return errs;}/* vector_of_vectors_test() * * Builds a vector of a vector of ints. Assuming an int array of size 9 integers, * and treating the array as a 3x3 2D array, this will grab the corners. * * MPICH1 fails this test because it converts the vectors into hvectors. * * Returns the number of errors encountered. */int vector_of_vectors_test(void){ MPI_Datatype inner_vector, inner_vector_copy; MPI_Datatype outer_vector; int nints, nadds, ntypes, combiner, *ints; MPI_Aint *adds = NULL; MPI_Datatype *types; int err, errs = 0; /* set up type */ err = MPI_Type_vector(2, 1, 2, MPI_INT, &inner_vector); if (err != MPI_SUCCESS) { if (verbose) fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs+1); return errs+1; } err = MPI_Type_vector(2, 1, 2, inner_vector, &outer_vector); if (err != MPI_SUCCESS) { if (verbose) fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs+1); return errs+1; } /* decode outer vector (get envelope, then contents) */ err = MPI_Type_get_envelope(outer_vector, &nints, &nadds, &ntypes, &combiner); if (err != MPI_SUCCESS) { if (verbose) fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs+1); return errs+1; } if (nints != 3) errs++; if (nadds != 0) errs++; if (ntypes != 1) errs++; if (combiner != MPI_COMBINER_VECTOR) errs++; if (verbose) { if (nints != 3) fprintf(stderr, "outer vector nints = %d; should be 3\n", nints); if (nadds != 0) fprintf(stderr, "outer vector nadds = %d; should be 0\n", nadds); if (ntypes != 1) fprintf(stderr, "outer vector ntypes = %d; should be 1\n", ntypes); if (combiner != MPI_COMBINER_VECTOR) fprintf(stderr, "outer vector combiner = %s; should be vector\n", combiner_to_string(combiner)); } if (errs) { if (verbose) fprintf(stderr, "aborting after %d errors\n", errs); return errs; } ints = malloc(nints * sizeof(*ints)); if (nadds) adds = malloc(nadds * sizeof(*adds)); types = malloc(ntypes * sizeof(*types)); /* get contents of outer vector */ err = MPI_Type_get_contents(outer_vector, nints, nadds, ntypes, ints, adds, types); if (ints[0] != 2) errs++; if (ints[1] != 1) errs++; if (ints[2] != 2) errs++; if (verbose) { if (ints[0] != 2) fprintf(stderr, "outer vector count = %d; should be 2\n", ints[0]); if (ints[1] != 1) fprintf(stderr, "outer vector blocklength = %d; should be 1\n", ints[1]); if (ints[2] != 2) fprintf(stderr, "outer vector stride = %d; should be 2\n", ints[2]); } if (errs) { if (verbose) fprintf(stderr, "aborting after %d errors\n", errs); return errs; } inner_vector_copy = types[0]; free(ints); if (nadds) free(adds); free(types); /* decode inner vector */ err = MPI_Type_get_envelope(inner_vector_copy, &nints, &nadds, &ntypes, &combiner); if (err != MPI_SUCCESS) { if (verbose) fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs+1); return errs+1; } if (nints != 3) errs++; if (nadds != 0) errs++; if (ntypes != 1) errs++; if (combiner != MPI_COMBINER_VECTOR) errs++; if (verbose) { if (nints != 3) fprintf(stderr, "inner vector nints = %d; should be 3\n", nints); if (nadds != 0) fprintf(stderr, "inner vector nadds = %d; should be 0\n", nadds); if (ntypes != 1) fprintf(stderr, "inner vector ntypes = %d; should be 1\n", ntypes); if (combiner != MPI_COMBINER_VECTOR) fprintf(stderr, "inner vector combiner = %s; should be vector\n", combiner_to_string(combiner)); } if (errs) { if (verbose) fprintf(stderr, "aborting after %d errors\n", errs); return errs; } ints = malloc(nints * sizeof(*ints)); if (nadds) adds = malloc(nadds * sizeof(*adds)); types = malloc(ntypes * sizeof(*types)); err = MPI_Type_get_contents(inner_vector_copy, nints, nadds, ntypes, ints, adds, types); if (ints[0] != 2) errs++; if (ints[1] != 1) errs++; if (ints[2] != 2) errs++; if (verbose) { if (ints[0] != 2) fprintf(stderr, "inner vector count = %d; should be 2\n", ints[0]); if (ints[1] != 1) fprintf(stderr, "inner vector blocklength = %d; should be 1\n", ints[1]); if (ints[2] != 2) fprintf(stderr, "inner vector stride = %d; should be 2\n", ints[2]); } if (errs) { if (verbose) fprintf(stderr, "aborting after %d errors\n", errs); return errs; } free(ints); if (nadds) free(adds); free(types); MPI_Type_free( &inner_vector_copy ); MPI_Type_free( &inner_vector ); MPI_Type_free( &outer_vector ); return 0;}/* optimizable_vector_of_basics_test() * * Builds a vector of ints. Count is 10, blocksize is 2, stride is 2, so this * is equivalent to a contig of 20. But remember...we should get back our * suboptimal values under MPI-2. * * Returns the number of errors encountered. */int optimizable_vector_of_basics_test(void){ MPI_Datatype parent_type; int nints, nadds, ntypes, combiner, *ints; MPI_Aint *adds = NULL; MPI_Datatype *types; int err, errs = 0; /* set up type */ err = MPI_Type_vector(10, 2, 2, MPI_INT, &parent_type); /* decode */ err = MPI_Type_get_envelope(parent_type, &nints, &nadds, &ntypes, &combiner); if (nints != 3) errs++; if (nadds != 0) errs++; if (ntypes != 1) errs++; if (combiner != MPI_COMBINER_VECTOR) errs++; if (verbose) { if (nints != 3) fprintf(stderr, "nints = %d; should be 3\n", nints); if (nadds != 0) fprintf(stderr, "nadds = %d; should be 0\n", nadds); if (ntypes != 1) fprintf(stderr, "ntypes = %d; should be 1\n", ntypes); if (combiner != MPI_COMBINER_VECTOR) fprintf(stderr, "combiner = %s; should be vector\n", combiner_to_string(combiner)); } ints = malloc(nints * sizeof(*ints)); if (nadds) adds = malloc(nadds * sizeof(*adds)); types = malloc(ntypes *sizeof(*types)); err = MPI_Type_get_contents(parent_type, nints, nadds, ntypes, ints, adds, types); if (ints[0] != 10) errs++; if (ints[1] != 2) errs++; if (ints[2] != 2) errs++; if (types[0] != MPI_INT) errs++; if (verbose) { if (ints[0] != 10) fprintf(stderr, "count = %d; should be 10\n", ints[0]); if (ints[1] != 2) fprintf(stderr, "blocklength = %d; should be 2\n", ints[1]); if (ints[2] != 2) fprintf(stderr, "stride = %d; should be 2\n", ints[2]); if (types[0] != MPI_INT) fprintf(stderr, "type is not MPI_INT\n"); } free(ints); if (nadds) free(adds); free(types); MPI_Type_free( &parent_type ); return errs;}/* indexed_of_basics_test(void) * * Simple indexed type. * * Returns number of errors encountered. */int indexed_of_basics_test(void){ MPI_Datatype parent_type; int s_count = 3, s_blocklengths[3] = { 3, 2, 1 }; int s_displacements[3] = { 10, 20, 30 }; int nints, nadds, ntypes, combiner, *ints; MPI_Aint *adds = NULL; MPI_Datatype *types; int err, errs = 0; /* set up type */ err = MPI_Type_indexed(s_count, s_blocklengths, s_displacements, MPI_INT, &parent_type); /* decode */ err = MPI_Type_get_envelope(parent_type, &nints, &nadds, &ntypes, &combiner); if (nints != 7) errs++; if (nadds != 0) errs++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -