在MPI中发送带有char[]的结构
Send struct with char[] in MPI
我正试图将一些数据从工作程序发送到C++中MPI程序中的主程序(排名为0)。目标是传递2个字符串和一个整数。为此,我创建了一个结构。
结构
它被称为单词,定义如下:
struct word
{
char word_name[str_size];
char url[str_size];
int counter;
};
/* Some important variables to the question */
MPI_Datatype mpi_word_type;
const int str_size = 200;
以前我通过char *
尝试过,但它不起作用,因为进程不共享相同的内存空间。
到目前为止,如果我将变量从char[]
更改为简单的char
并尝试使用一个示例,我就可以发送结构了。如上所述,我无法消除分割错误错误。
发送零件-工人
我首先创建并填充一个示例结构,然后首先发送结构的大小,其次发送结构本身的大小。像这样:
word word_col;
std::string tmp = "somename";
strcpy(word_col.word_name, tmp.c_str());
std::string tmp2 = "someurl";
strcpy(word_col.url, tmp2.c_str());
word_col.counter = 10;
int size = sizeof(word_col);
MPI::COMM_WORLD.Send(&size, 1, MPI::INT, 0, 1);
MPI::COMM_WORLD.Send(&word_col, size, mpi_word_type, 0, 1);
接收部件-主
const int nitems = 3;
int blocklengths[3] = { str_size, str_size, 1 };
MPI_Datatype types[3] = { MPI::CHAR, MPI::CHAR, MPI::INT };
MPI_Aint offsets[3];
offsets[0] = (MPI_Aint) offsetof(struct word, word_name);
offsets[1] = (MPI_Aint) offsetof(struct word, url);
offsets[2] = (MPI_Aint) offsetof(struct word, counter);
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_word_type);
MPI_Type_commit(&mpi_word_type);
...
for(...)
{
word word_col;
int size;
MPI::COMM_WORLD.Recv(&size, 1, MPI::INT, MPI::ANY_TAG, 1, status);
MPI::COMM_WORLD.Recv(&word_col, size, mpi_word_type, MPI::ANY_TAG, 1, status);
}
我已经为此挣扎了几个小时,我看到了很多关于这个的例子和其他问题,但我不知道这里的问题是什么。
这是错误的编程。您有未分配和未初始化的指针,您正试图将数据推送到该指针。您有两种选择:要么将您的结构定义为:
const int str_size = 200;
struct word
{
char word_name[str_size]; // fixed sized char array
char url[str_size]; // fixed sized char array
int counter;
};
或者,
const int str_size = 200;
struct word
{
char *word_name; /
char *url;
int counter;
Word() {
word_name = new char[str_size];
url = new char[str_size];
}
~Word() {
delete [] word_name;
delete [] url;
}
};
其思想是,您需要为这些变量分配内存此外,在接收时,您使用了:
MPI::COMM_WORLD.Recv(&word_col, size, mpi_word_type, MPI::ANY_TAG, 1, status);
它不应该是下面的样子吗?
MPI::COMM_WORLD.Recv(&word_col, sizeof(word_col), mpi_word_type, MPI::ANY_TAG, 1, status);
相关文章:
- 具有结构成员char数组的sscanf
- 带有 char[] 字段的 POD 结构的 constexpr 构造
- 将结构 std::memcpy 转换为具有足够容量的 std::vector 是未定义的行为<char>吗?
- 成员引用基类型 'char' 不是 C++ 中的结构或联合
- C 结构具有CHAR数组以不寻常的方式初始化为零
- w/ w/结构带char缓冲液的静态初始化[]
- 是否很好地定义了强制转换为仅由 char[] 组成的结构并从该数组读取?
- 如何在结构中序列化unique_ptr<char[]>
- char*在结构内存分配中
- Realloc char* 内部结构
- C++将结构强制转换为 std::矢量<char>内存对齐
- 将包含位字段和动态数据的结构复制到 Char 数组缓冲区中
- C MPI创建并发送具有字段char [16]和整数的结构数组
- 数据结构对齐:为什么Char和STD :: String的数组不同
- 使用 JNA 访问包含 vector<char* 的结构>
- 将char*转换为C 的结构
- 使用 char* 将结构传递给 C# 中的 C++ dll 函数
- 清空包含char*的结构数组
- 如何将STD :: String的值复制到Char数组类型的结构成员
- 为什么"char"结构类型中的编程实践不好?