c++ - MPI,更大的阵列

标签 c++ c arrays mpi

我有一个代码...并且我正在尝试在 16 个 cpu 上运行。问题是..我有一个数组 qith 索引为 15000...如果我尝试使用 15000 , mpi 无法运行。到目前为止...我成功地运行了 800 个数组。我可以以某种方式制作...该程序可以处理更大的数组吗?我尝试将数据类型设置为 long int ..但显然 mpi_com_rank 和 mpi_com_size 不接受这种数据类型。 抱歉,如果我问一些愚蠢的问题...但我真的需要帮助。多谢。这是示例代码:

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "iostream"
#include "mpi.h"
#define size 15000

int main(int argc, char *argv[] ) {

    int numprocs, rank, chunk_size, i;
    int max, mymax,rem;
    int array[size];
    MPI_Status status;

    MPI_Init( &argc,&argv);
    MPI_Comm_rank( MPI_COMM_WORLD, &rank);
    MPI_Comm_size( MPI_COMM_WORLD, &numprocs);

    printf("Hello from process %d of %d \n",rank,numprocs);
    chunk_size = size/numprocs;
        rem = size%numprocs;

    if (rank == 0) {
    /* Initialize Array */
        printf("REM %d \n",rem);
        for(i=0;i<size;i++) {
            array[i] = i;
        }
    /* Distribute Array */
        for(i=1;i<numprocs;i++) {
            if(i<rem ) {
            MPI_Send(&array[i*chunk_size],chunk_size+1, MPI_INT, i, 1, MPI_COMM_WORLD);
            } else {
            MPI_Send(&array[i*chunk_size],chunk_size, MPI_INT, i, 1, MPI_COMM_WORLD);
            }
        }
    }
    else {
        MPI_Recv(array, chunk_size, MPI_INT, 0,1,MPI_COMM_WORLD,&status);
    }
   /*Each processor has a chunk, now find local max */
   mymax = array[0]; 
   for(i=1;i<chunk_size;i++) {
        if(mymax<array[i]) {
            mymax = array[i];
        }
    }
    printf("Array els 1-5 for rank %d: %d %d %d %d %d\n",rank,array[0],array[1],array[2],array[3],array[4]);
    printf("Last 5 Array els for rank %d: %d %d %d %d %d\n",rank,array[chunk_size-5],array[chunk_size-4],array[chunk_size-3],array[chunk_size-2],array[chunk_size-1]);
    printf("The Max for rank %d is: %d\n",rank,mymax);

   /*Send local_max back to master */ 
    if (rank == 0) {
      max = mymax; //Store rank 0 local maximum
        for(i=1;i<numprocs;i++) {
            MPI_Recv(&mymax,1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD,&status);
            if(max<mymax) max = mymax;
        }
        printf("The Max is: %d",max);
    }
    else {
        MPI_Send(&mymax, 1, MPI_INT, 0,1,MPI_COMM_WORLD);
    }
    MPI_Finalize();
    std::cin.ignore();
    return 0;
}

我正在使用 Visual Studio 编译程序..这就是我拥有 iostream 库的原因(所以我可以使用 cin.ignore..否则我的控制台窗口会消失在稀薄的空气中...即使我将其设置为保持打开状态来自 VisualStudio 的屏幕)。在这个公式中..我最多可以在 5 个线程上运行。

-np 5“$(目标路径)”

超过 5.. 失败。如果我降低大小(从 15000 到 500 ..我可以使用 16 个线程) -np 16 "$(目标路径)" 有人怀疑为什么吗?任何建议都很好。

最佳答案

您正在将数组分配到堆栈。您需要将它们分配到堆中。请参阅此问题和答案,了解 stack and heap 的详细概述。 .

您需要使用malloc and free如果 C 或 new and delete在 C++ 中。

关于c++ - MPI,更大的阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623578/

相关文章:

c - 卢阿C : How would I use the Lua source code to create a Lua interpreter that will execute given blocks of Lua code?

c - 无法从 C 中的 argv[1] 获取文件名

javascript - 在 Javascript 中通过键数组过滤对象

c++ - void 指针和 NULL 指针有什么区别?

c++ - 为什么 std::string::max_size 不是编译时常量?

C:使用 for 循环、ceil 语句和奇数与偶数语句进行迭代

python - main 中定义的 Numpy 数组在函数中无法识别

Java:带有char数组的println给出乱码

c++ - 保留最小完美哈希函数的顺序

c++ - 从标准输入中捕获字符而无需等待按下回车键