📄 transpose5.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */#include "mpi.h" #include "stdio.h"#include "mpitest.h"/* This does a transpose-cum-accumulate operation. Uses vector and hvector datatypes (Example 3.32 from MPI 1.1 Standard). Run on 2 processes */ #define NROWS 100#define NCOLS 100int main(int argc, char *argv[]) { int rank, nprocs, A[NROWS][NCOLS], i, j; MPI_Win win; MPI_Datatype column, xpose; int errs = 0; MTest_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&rank); if (nprocs != 2) { printf("Run this program with 2 processes\n"); MPI_Abort(MPI_COMM_WORLD,1); } if (rank == 0) { for (i=0; i<NROWS; i++) for (j=0; j<NCOLS; j++) A[i][j] = i*NCOLS + j; /* create datatype for one column */ MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column); /* create datatype for matrix in column-major order */ MPI_Type_hvector(NCOLS, 1, sizeof(int), column, &xpose); MPI_Type_commit(&xpose); MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win); MPI_Win_fence(0, win); MPI_Accumulate(A, NROWS*NCOLS, MPI_INT, 1, 0, 1, xpose, MPI_SUM, win); MPI_Type_free(&column); MPI_Type_free(&xpose); MPI_Win_fence(0, win); } else { /* rank = 1 */ for (i=0; i<NROWS; i++) for (j=0; j<NCOLS; j++) A[i][j] = i*NCOLS + j; MPI_Win_create(A, NROWS*NCOLS*sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win); MPI_Win_fence(0, win); MPI_Win_fence(0, win); for (j=0; j<NCOLS; j++) { for (i=0; i<NROWS; i++) { if (A[j][i] != i*NCOLS + j + j*NCOLS + i) { if (errs < 50) { printf("Error: A[%d][%d]=%d should be %d\n", j, i, A[j][i], i*NCOLS + j + j*NCOLS + i); } errs++; } } } if (errs >= 50) { printf("Total number of errors: %d\n", errs); } } MPI_Win_free(&win); MTest_Finalize(errs); MPI_Finalize(); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -