Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在单个函数上应用MPI_C_Mpi - Fatal编程技术网

C 如何在单个函数上应用MPI

C 如何在单个函数上应用MPI,c,mpi,C,Mpi,我需要在两个CPU上并行运行代码中的函数generator。该项目有两个文件main.c和cartes.c,它是按顺序运行的 main.c文件包含一个for循环,用于调用函数generator main.c: MPI_Init(&argc, &argv); int id; MPI_Comm_rank(MPI_COMM_WORLD, &id); . . . for (iter_sec = 0; iter_sec < long_of_seq; iter_sec++) {

我需要在两个CPU上并行运行代码中的函数
generator
。该项目有两个文件
main.c
cartes.c
,它是按顺序运行的

main.c
文件包含一个for循环,用于调用函数
generator

main.c

MPI_Init(&argc, &argv);
int id;
MPI_Comm_rank(MPI_COMM_WORLD, &id);
.
.
.
for (iter_sec = 0; iter_sec < long_of_seq; iter_sec++) { //
    uint64_t Xg1 = generator(&K[0], iter_sec, ratio1, ratio2, ratio3, ratio4, m1, m2, id);
    sequence[iter_sec] = Xg1;
    .
    .
}
useTheSequenceArrayResultFromTheForLoopToCaclulateAndPrintTheFinalResult(sequence);
.
.
.
cartes.c

.
.
.
uint64_t generator(key* K, int iter_sec, double ratio1, double ratio2, double ratio3, double ratio4, uint64_t m1, uint64_t m2, int id){
    K->X_s = someFunction(...);
    K->X_s = someOtherFunction(...);

    Xresult = K->X_p ^ K->X_s;
    return Xresult;
}
.
.
.
我想做的是在两个CPU上计算函数
生成器的结果。我将处理器的列组id传递给函数
生成器
。如果列组id为
0
我需要调用
someFunction
,如果列组id为
1
我需要调用
someOtherFunction
。最后,我需要从
someFunction
someOtherFunction
返回结果的异或
^

我想做的是:

.
.
.
uint64_t generator(key* K, int iter_sec, double ratio1, double ratio2, double ratio3, double ratio4, uint64_t m1, uint64_t m2, int id){
    if (id == 0) {
        K->X_s = someFunction(...);
    }
    else if(id == 1){
        K->X_s = someOtherFunction(...);
    }

    Xresult = K->X_p ^ K->X_s;
    return Xresult;
}
.
.
.
但是上面修改的函数不起作用

当我尝试运行代码时:

mpicc main.c -o saving
mpirun -np 4 ./saving
整个代码运行四次,
生成器
的结果不会返回到for循环,也不会存储在
序列
数组中

如何修改
sequence
函数,使其在不同的CPU上执行
someFunction
someOtherFunction
,并将两者的异或结果返回到主文件中的
for循环

填充完
序列
数组后,我将把它传递给函数
使用Orloop中的SequencearrayResults,并打印最终结果
,以计算最终结果。

您并行运行了四个程序实例,但您忘记了使用
MPI\u Reduce(…)
在一个位置收集所有结果。您可以在下面看到一个小示例,说明如何做到这一点。但你必须决定在哪里收集结果。您可以在任何地方收集它们,但出于性能原因,最好在循环之外收集它们

#include <stdio.h>
#include <mpi.h>

#define RESULT_1 0x55
#define RESULT_2 0xAA
#define UNDEFINED 0xBB

int calc(int id) {
    if (id == 0) {
        return RESULT_1;
    }
    return RESULT_2;
}

int main (int argc, char* argv[])
{
    int id, local, global = UNDEFINED;

    MPI_Init (&argc, &argv);
    MPI_Comm_rank (MPI_COMM_WORLD, &id);

    local = calc(id);

    MPI_Reduce(&local, &global, 1, MPI_INT, MPI_BXOR, 0, MPI_COMM_WORLD);

    printf("at the moment: local=%02x, global=%02x\n", local, global);

    if (!id) {
        printf("Result is: %02x\n", global);
    }

    MPI_Finalize();
    return 0;
}

请提供一个。您没有解释OP如何将
calc
的结果存储在一个数组中,以便稍后在其函数中使用它们
使用Orloop中的SequencearrayResults到Aclulate并打印最终结果
@Yazan,因此没有理由重新键入所有文档。我添加了对主要方法的描述和一个数组示例。这个网站是一本食谱。不是餐馆。
#include <stdio.h>
#include <mpi.h>

#define RESULT_1 0x55
#define RESULT_2 0xAA
#define UNDEFINED 0xBB

int calc(int id) {
    if (id == 0) {
        return RESULT_1;
    }
    return RESULT_2;
}

int main (int argc, char* argv[])
{
    int id, local, global = UNDEFINED;

    MPI_Init (&argc, &argv);
    MPI_Comm_rank (MPI_COMM_WORLD, &id);

    local = calc(id);

    MPI_Reduce(&local, &global, 1, MPI_INT, MPI_BXOR, 0, MPI_COMM_WORLD);

    printf("at the moment: local=%02x, global=%02x\n", local, global);

    if (!id) {
        printf("Result is: %02x\n", global);
    }

    MPI_Finalize();
    return 0;
}
#include <stdio.h>
#include <mpi.h>

#define SIZE_OF_ARRAY 16

#define RESULT_1 0x55
#define RESULT_2 0xAA
#define UNDEFINED 0xBB

int calc(int id) {
    if (id == 0) {
        return RESULT_1;
    }
    return RESULT_2;
}

int main (int argc, char* argv[])
{
    int i, id;
    static int local[SIZE_OF_ARRAY];
    static int global[SIZE_OF_ARRAY];

    MPI_Init (&argc, &argv);
    MPI_Comm_rank (MPI_COMM_WORLD, &id);

    for(i = 0; i < SIZE_OF_ARRAY; i++) {
        local[i] = calc(id);   
        global[i] = UNDEFINED; // just to show that this value will be altered
    }

    // for an instance with (id == 0), local elements will have a value of 0x55
    // for an instance with (id != 0), local elements will have a value of 0xAA
    // the value of elements of global is undefined here

    MPI_Reduce(local, global, SIZE_OF_ARRAY, MPI_INT, MPI_BXOR, 0, MPI_COMM_WORLD);

    // after this call the values of elements of global are undefined if (id != 0)
    // if (id == 0) the values of elements of global will be combination of
    // appropriate elements of local arrays from different instances
    // let's denote "var_0" the variable "var" from the instance #0
    // we will get:
    // global_0[i] = local_0[i] ^ local_1[i] ^ local_2[i] ^ local_3[i]

    if (!id) {
        // use the sequence array result from the loop here
        printf("Result is: [%02x", global[0]);
        for(i = 1; i < SIZE_OF_ARRAY; i++) {
            printf(", %02x", global[i]);
        }
        printf(" ]\n");
    }

    MPI_Finalize();
    return 0;
}