OpenMPI Master Slave

时间:2015-08-28 10:39:43

标签: mpi openmpi

我试图将字符串str_data1分开并将其发送到MPI_COMM_WORLD中的从属处理器,但我在从属服务器上收到错误。

错误如下所示:

  

2

     

2

     

3
    �E0��E0����E0O�

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

int main(int argc, char* argv[]) {
int rank;
int p;
MPI_Status status;
int msg_size = 0;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &p);

char *str_data1 = "Samsung Galaxy Tab S 10.5 Wi-Fi, Tablet PC Android";

int len1 = strlen(str_data1), i;
char *str1[len1];
char *a[len1];

if (rank == 0) {

    char *ds = strdup(str_data1);
    int n = 0;
    a[n] = strtok(ds, " ,");
    while (a[n] && n < len1) {
        a[++n] = strtok(NULL, " ,");
    }

    int chunk = n / p;
    int str_size = chunk;
    for (i = 1; i < p; i++) {
        if (i == p - 1) {
            str_size = n - chunk * i;
        }
        MPI_Send(&str_size, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
        MPI_Send(&a, str_size + 1, MPI_CHAR, i, 0, MPI_COMM_WORLD);
    }

} else {

    MPI_Recv(&msg_size, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
    char messagsg_size];
    printf(" \n  %d ", msg_size);
    MPI_Recv(&message, msg_size + 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD,
            &status);
    printf(" \n  %s ", message);
}
MPI_Finalize();

return 0;
}

有没有人有任何线索我做错了什么?谢谢。

2 个答案:

答案 0 :(得分:0)

一些较新的编译器会对这些事情给你很好的警告。自Clang添加此内容以来,它一直非常好。如果你要使用那个编译器,你会看到:

$ mpic++ asdf.c
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
asdf.c:17:23: warning: conversion from string literal to 'char *' is deprecated [-Wc++11-compat-deprecated-writable-strings]
    char *str_data1 = "Samsung Galaxy Tab S 10.5 Wi-Fi, Tablet PC Android";
                      ^
asdf.c:39:22: warning: argument type 'char *(*)[len1]' doesn't match specified 'MPI' type tag that requires 'char *' [-Wtype-safety]
            MPI_Send(&a, str_size + 1, MPI_CHAR, i, 0, MPI_COMM_WORLD);
                     ^~                ~~~~~~~~
asdf.c:47:18: warning: argument type 'char (*)[msg_size]' doesn't match specified 'MPI' type tag that requires 'char *' [-Wtype-safety]
        MPI_Recv(&message, msg_size + 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD,
                 ^~~~~~~~                ~~~~~~~~
3 warnings generated.

这表明您为字符数组使用了错误的类型。你应该只使用字符数组:

char a[len1];

或字符指针:

char *a;

无论哪种方式,您都需要进行一些代码返工才能使其正常工作。具体来说,这一节:

char *ds = strdup(str_data1);
int n = 0;
a[n] = strtok(ds, " ,");
while (a[n] && n < len1) {
    a[++n] = strtok(NULL, " ,");
}

我不认为字符串标记符正在按照您的想法执行,因为它会一遍又一遍地覆盖它。

答案 1 :(得分:0)

谢谢Wesly ..我厌倦了结构......看起来像是在工作......

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

struct tokan {
char buff[30];
} t[50];

int main(int argc, char* argv[]) {
int rank;
int p;
MPI_Status status;
int msg_size = 0, i = 0, j = 0, msg_size1 = 0;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Comm_size(MPI_COMM_WORLD, &p);
int n = 0, m = 0, k = 0, N = 0, M = 0;
char *str1, *str2, *str_data2, *str_data1;

if (rank == 0) {

    str_data1 =
            "processes in this communicator will now abort, and potentially your MPI job";

    str_data2 =
            "The behavior is undefined if lhs or rhs are not pointers to null-terminated byte strings";

    str1 = (char *) malloc(strlen(str_data1) * sizeof(char));
    str2 = (char *) malloc(strlen(str_data2) * sizeof(char));

    strcpy(str1, str_data1);
    strcpy(str2, str_data2);

    int len;
    M = strlen(str2);
    N = strlen(str1);

    char *ptr;
    k = 0;
    char *ds = strdup(str_data1);
    ptr = strtok(ds, " ,");
    while (ptr != NULL) {
        ++n;
        len = strlen(ptr);
        for (j = 0; j < len; j++) {
            t[k].buff[j] = *(ptr + j);
        }
        //printf(" %s ", t[k].buff);
        k++;
        ptr = strtok(NULL, " ,");
    }
    int chunk = n / p;
    int str_size = chunk, cnt = 0;
    j = chunk;
    for (i = 1; i < p; i++) {
        if (i == p - 1) {
            str_size = n - chunk * i;
        }
        MPI_Send(&str_size, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
        for (cnt = 0; cnt < str_size; cnt++) {
            /*printf("process 0: %c %d %d %d %d %d %d\n", t[j].buff, str_size, n,
             chunk, cnt, j, l);*/
            MPI_Send(t[j].buff, 100, MPI_CHAR, i, 0, MPI_COMM_WORLD);
            j++;
        }
    }

    str_size = chunk;
    for (i = 1; i < p; i++) {
        MPI_Send(&M, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
        //printf("process 0: %s %d %d %d %d\n", str2, n, chunk, cnt, j);
        MPI_Send(str2, M, MPI_CHAR, i, 0, MPI_COMM_WORLD);
    }

} else {

    MPI_Recv(&msg_size, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
    int k;
    for (k = 0; k < msg_size; k++) {
        MPI_Recv(t[k].buff, 100, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
        printf(" \nstr1:  %s %d %d  %d\n", t[k].buff, rank, msg_size, k);
    }

    printf("***************");
    MPI_Recv(&M, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
    str2 = (char *) malloc((M + 1) * sizeof(char));
    MPI_Recv(str2, M, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
    str2[M] = '\0';
    printf(" \nstr2:  %s %d %d %d \n", str2, rank, k, M);
}
if (rank == 0)
    free(str1);
free(str2);
MPI_Finalize();

return 0;
}