gpt4 book ai didi

c - 在生成过程中获取值

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:01 25 4
gpt4 key购买 nike

我正在尝试使用集体 MPI 函数在我生成的进程中获取值。

在这种情况下,我有一个 N*N 矩阵,我想将每一行传递给每个进程。获取每个进程中的值并将它们的值相加。

我正在使用这个例子:

MPI_Scatter of 2D array and malloc

主要

int main(int argc, char *argv[]){
int *n, range, i, j, dato, resultado;
int *matriz;
char *nombre_esclave="esclavo";

//MPI Section
int rank, size;
MPI_Comm hijos;
MPI_Status status;



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

matriz = createMatrix(N, N);
printArray(matriz, N * N);

//Child process
MPI_Comm_spawn("slave", MPI_ARGV_NULL, N, MPI_INFO_NULL, 0, MPI_COMM_SELF, &hijos, MPI_ERRCODES_IGNORE);


// received row will contain N integers
int *procRow = malloc(sizeof(int) * N);

MPI_Scatter(matriz, N, MPI_INT, // send one row, which contains N integers
procRow, N, MPI_INT, // receive one row, which contains N integers
MPI_ROOT, hijos);



MPI_Finalize();
return 0;
}

在奴隶中

奴隶

   MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
MPI_Comm_size(MPI_COMM_WORLD, &size);


MPI_Comm_get_parent(&parent);

if (parent != MPI_COMM_NULL) {
printf("This is a child process\n");
}

//number of processes in the remote group of comm (integer)
MPI_Comm_remote_size(parent, &size);


int *procRow = malloc(sizeof(int) * N);

//UNABLE TO GET VALUES FROM THE PARENT
//I need to sum old the values y every portion of the matrix
//passed to every child process
MPI_Reduce(procRow, &resultado_global, N, MPI_INT, MPI_SUM, 0, parent);

更新 enter image description here

我用 MPI_Comm_spawn 创建了 3 个 child 。在每个 child 中,我都想得到一行矩阵(我在 master 中使用 scatter )。后来我使用 MPI_Reduce 对 child 中的每一行求和(这就是我说获取值的原因)。

更新 2

在 slave 上,我修改了代码,并在每个进程中获取了行。

if (parent != MPI_COMM_NULL) {


//number of processes in the remote group of comm (integer)
MPI_Comm_remote_size(parent, &size_remote);

int *matrix = malloc(sizeof(int) * size);
int *procRow = malloc(sizeof(int) * size);



MPI_Scatter(matrix, N, MPI_INT,procRow, N, MPI_INT,0, parent);

//procRow values correctly from each row of the matrix

if (procRow != NULL) {
printf("Process %d; %d %d %d \n", pid, procRow[0], procRow[1], procRow[2]);
}

//Unable to sum each row
MPI_Reduce(procRow, &resultado_global, size, MPI_INT, MPI_SUM, ROOT, parent);
//MPI_Reduce(procRow, &resultado_global, size, MPI_INT, MPI_SUM, ROOT, MPI_COMM_WORLD);

}

更新 3(已解决)

在奴隶中

if (parent != MPI_COMM_NULL) {

//number of processes in the remote group of comm (integer)
MPI_Comm_remote_size(parent, &size_remote);

int *matrix = malloc(sizeof(int) * size);
int *procRow = malloc(sizeof(int) * size);


MPI_Scatter(matrix, N, MPI_INT, procRow, N, MPI_INT, 0, parent);



if (procRow != NULL) {
printf("Process %d; %d %d %d \n", pid, procRow[0], procRow[1], procRow[2]);
sumaParcial=0;
for (int i = 0; i < N; i++)
sumaParcial = sumaParcial + procRow[i];
}



MPI_Reduce(&sumaParcial, &resultado_global, 1, MPI_INT, MPI_SUM, ROOT, parent);


}

在母版中

  // received row will contain N integers
int *procRow = malloc(sizeof(int) * N);

MPI_Scatter(matriz, N, MPI_INT, // send one row, which contains N integers
procRow, N, MPI_INT, // receive one row, which contains N integers
MPI_ROOT, hijos);


MPI_Reduce(&sumaParcial, &resultado_global, 1, MPI_INT, MPI_SUM, MPI_ROOT, hijos);

printf("\n GLOBAL RESULT :%d\n",resultado_global);

有什么想法吗?谢谢

最佳答案

根据编辑,我认为散点图工作正常。

您的主要困惑似乎是关于 MPI_Reduce。它不做任何局部减少。根据您的图形,您希望在从站中的 0、1、2 等级中获得值 6、15、24。这完全无需 MPI 即可完成,只需遍历本地行即可。

行上的 MPI_Reduce 将导致根具有 [12, 15, 18]。如果你只想要总和 45 在 slaves 的根部,你应该首先在本地汇总值,然后 MPI_Reduce 来自每个等级的单个值到单个全局值.

关于c - 在生成过程中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41956411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com