Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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++ 在C+中传递给MPI的结构+;_C++_Mpi - Fatal编程技术网

C++ 在C+中传递给MPI的结构+;

C++ 在C+中传递给MPI的结构+;,c++,mpi,C++,Mpi,我试图为MPI创建一个自定义数据类型,并将d_类型的结构从根进程传递到其他进程,但我遇到了一个分段错误和以下错误。我想稍后制作一个结构向量,然后将该向量从根进程传递到其他进程 无效的MIT-MAGIC-COOKIE-1密钥[blackbirdx-H170-Gaming-3:11787]***处理接收到的信号*** [blackbirdx-H170-3:11787]信号:分段故障(11) [blackbirdx-H170-3:11787]信号代码:地址未映射(1) [blackbirdx-H170

我试图为MPI创建一个自定义数据类型,并将d_类型的结构从根进程传递到其他进程,但我遇到了一个分段错误和以下错误。我想稍后制作一个结构向量,然后将该向量从根进程传递到其他进程

无效的MIT-MAGIC-COOKIE-1密钥[blackbirdx-H170-Gaming-3:11787]***处理接收到的信号***

[blackbirdx-H170-3:11787]信号:分段故障(11)

[blackbirdx-H170-3:11787]信号代码:地址未映射(1)

[blackbirdx-H170-Gaming-3:11787]在地址0x7ffd2daa9dd8失败

#include <iostream>
#define HAVE_STRUCT_TIMESPEC
#include <mpi.h>
#include <time.h>
#include <queue>
#include <string>
#include <sstream>
#include <fstream>
#include <thread>  
#include <chrono> 
#include <vector>
using namespace std;
struct d_type{
    string timestamp;
    int lightpostID;
    int carTraffic;
};
class trafficData
{
public:
    string timestamp;
    int lightpostID;
    int carTraffic;

    trafficData(string timestamp, int lightpostID, int carTraffic);
};

trafficData::trafficData(string timestamp, int lightpostID, int carTraffic)
{
    this->timestamp = timestamp;
    this->lightpostID = lightpostID;
    this->carTraffic = carTraffic;
}
#define TRAFFIC_LIGHTS 5 
#define SIMULATION_TIME 3
#define MASTER_RANK 0
MPI_Status status;
#define CLASS_MASTER 1
#define CLASS_WORKER 2
int counter =0;
void generateTraffic();
// Read into queue.
void dataProducer();
// Read data from queue and sort it into list.
void dataConsume();
queue<trafficData> dataQueue;
vector<trafficData> sortedTraffic;
vector<trafficData> hourlyTraffic;
vector<string> movementLog;

int main(int argc, char *argv[])
{
    int sizeCommunicator,rankProcess,idProcess,offsetTotal;
    int offsetRow,workProcess,remainder,whole_part;
    int message_tag;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &sizeCommunicator);
    MPI_Comm_rank(MPI_COMM_WORLD, &rankProcess);
    if(sizeCommunicator < 2){
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    MPI_Datatype string_dt;
    MPI_Type_contiguous(6, MPI_CHAR, &string_dt);
    MPI_Type_commit(&string_dt);
    
    MPI_Datatype traffic_type;
    const int nitems=3;
    int blocklengths[nitems] = {1,1,1};
    const MPI_Aint displacements[nitems] = { offsetof(d_type, timestamp), offsetof(d_type, lightpostID), offsetof(d_type, carTraffic) };
    MPI_Datatype types[nitems] = {  string_dt ,MPI_INT, MPI_INT};
    MPI_Type_create_struct(nitems, blocklengths, displacements, types, &traffic_type);
    MPI_Type_commit(&traffic_type);
    MPI_Barrier(MPI_COMM_WORLD);

    if(rankProcess == MASTER_RANK) {
        workProcess = sizeCommunicator - 1;
        string str="11:55";
        d_type buffer;
        buffer.timestamp="11:00";
        buffer.lightpostID=1;
        buffer.carTraffic=500;
    
        message_tag = CLASS_MASTER;
            for(idProcess = 1; idProcess <= workProcess; idProcess ++ ){
              MPI_Send(&buffer, 1, traffic_type, idProcess, message_tag, MPI_COMM_WORLD);
            }
        message_tag = CLASS_WORKER;
            for(idProcess = 1; idProcess <= workProcess; idProcess ++){
                MPI_Recv(&str[0], 1, string_dt, idProcess, message_tag, MPI_COMM_WORLD, &status);
                cout<<str<<endl;
            }
    }
    if(rankProcess != MASTER_RANK){
        string str;
        d_type data;
        message_tag = CLASS_MASTER;
        MPI_Recv(&data, 1, traffic_type, MASTER_RANK, message_tag, MPI_COMM_WORLD, &status);

        
       //cout<<data.timestamp<<endl;
        // cout<<str<<endl;
        str="gone";
        message_tag = CLASS_WORKER;
        MPI_Send(&str[0], 1, string_dt, MASTER_RANK, message_tag, MPI_COMM_WORLD);
    }
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();
    return 0;
}
[黑鸟X-H170-3:11788]***处理接收到的信号***

[黑鸟X-H170-3:11789]***处理接收到的信号***

[blackbirdx-H170-3:11789]信号:分段故障(11)

[blackbirdx-H170-3:11789]信号代码:地址未映射(1)

[blackbirdx-H170-Gaming-3:11789]在地址0x7ffd2daa9dd8失败

#include <iostream>
#define HAVE_STRUCT_TIMESPEC
#include <mpi.h>
#include <time.h>
#include <queue>
#include <string>
#include <sstream>
#include <fstream>
#include <thread>  
#include <chrono> 
#include <vector>
using namespace std;
struct d_type{
    string timestamp;
    int lightpostID;
    int carTraffic;
};
class trafficData
{
public:
    string timestamp;
    int lightpostID;
    int carTraffic;

    trafficData(string timestamp, int lightpostID, int carTraffic);
};

trafficData::trafficData(string timestamp, int lightpostID, int carTraffic)
{
    this->timestamp = timestamp;
    this->lightpostID = lightpostID;
    this->carTraffic = carTraffic;
}
#define TRAFFIC_LIGHTS 5 
#define SIMULATION_TIME 3
#define MASTER_RANK 0
MPI_Status status;
#define CLASS_MASTER 1
#define CLASS_WORKER 2
int counter =0;
void generateTraffic();
// Read into queue.
void dataProducer();
// Read data from queue and sort it into list.
void dataConsume();
queue<trafficData> dataQueue;
vector<trafficData> sortedTraffic;
vector<trafficData> hourlyTraffic;
vector<string> movementLog;

int main(int argc, char *argv[])
{
    int sizeCommunicator,rankProcess,idProcess,offsetTotal;
    int offsetRow,workProcess,remainder,whole_part;
    int message_tag;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &sizeCommunicator);
    MPI_Comm_rank(MPI_COMM_WORLD, &rankProcess);
    if(sizeCommunicator < 2){
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    MPI_Datatype string_dt;
    MPI_Type_contiguous(6, MPI_CHAR, &string_dt);
    MPI_Type_commit(&string_dt);
    
    MPI_Datatype traffic_type;
    const int nitems=3;
    int blocklengths[nitems] = {1,1,1};
    const MPI_Aint displacements[nitems] = { offsetof(d_type, timestamp), offsetof(d_type, lightpostID), offsetof(d_type, carTraffic) };
    MPI_Datatype types[nitems] = {  string_dt ,MPI_INT, MPI_INT};
    MPI_Type_create_struct(nitems, blocklengths, displacements, types, &traffic_type);
    MPI_Type_commit(&traffic_type);
    MPI_Barrier(MPI_COMM_WORLD);

    if(rankProcess == MASTER_RANK) {
        workProcess = sizeCommunicator - 1;
        string str="11:55";
        d_type buffer;
        buffer.timestamp="11:00";
        buffer.lightpostID=1;
        buffer.carTraffic=500;
    
        message_tag = CLASS_MASTER;
            for(idProcess = 1; idProcess <= workProcess; idProcess ++ ){
              MPI_Send(&buffer, 1, traffic_type, idProcess, message_tag, MPI_COMM_WORLD);
            }
        message_tag = CLASS_WORKER;
            for(idProcess = 1; idProcess <= workProcess; idProcess ++){
                MPI_Recv(&str[0], 1, string_dt, idProcess, message_tag, MPI_COMM_WORLD, &status);
                cout<<str<<endl;
            }
    }
    if(rankProcess != MASTER_RANK){
        string str;
        d_type data;
        message_tag = CLASS_MASTER;
        MPI_Recv(&data, 1, traffic_type, MASTER_RANK, message_tag, MPI_COMM_WORLD, &status);

        
       //cout<<data.timestamp<<endl;
        // cout<<str<<endl;
        str="gone";
        message_tag = CLASS_WORKER;
        MPI_Send(&str[0], 1, string_dt, MASTER_RANK, message_tag, MPI_COMM_WORLD);
    }
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();
    return 0;
}
#包括
#定义HAVE_STRUCT_TIMESPEC
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构d_类型{
字符串时间戳;
国际灯柱;
国际货运;
};
类流量数据
{
公众:
字符串时间戳;
国际灯柱;
国际货运;
trafficData(字符串时间戳、int lightpostID、int carTraffic);
};
trafficData::trafficData(字符串时间戳、int-lightpostID、int-carTraffic)
{
此->时间戳=时间戳;
此->lightpostID=lightpostID;
此->carTraffic=carTraffic;
}
#定义交通灯5
#定义模拟时间3
#定义MASTER_秩0
MPI_状态;
#定义类_MASTER 1
#定义类别2
int计数器=0;
void generateTraffic();
//读入队列。
void dataProducer();
//从队列中读取数据并将其排序到列表中。
void dataConsume();
队列数据队列;
矢量分类交通;
向量小时交通;
矢量运动测井;
int main(int argc,char*argv[])
{
int-sizeCommunicator、rankProcess、idProcess、offsetTotal;
int offsetRow、工作过程、余数、全部或部分;
int message_标签;
MPI_Init(&argc,&argv);
MPI_通讯大小(MPI_通讯世界和大小通讯器);
MPI通信等级(MPI通信世界和rankProcess);
if(大小通讯器<2){
MPI_中止(MPI_通信世界,1);
}
MPI_数据类型字符串_dt;
MPI_类型_连续(6,MPI_字符和字符串_dt);
MPI_Type_commit(&string_dt);
MPI_数据类型流量_类型;
常数int nitems=3;
int blocklength[nitems]={1,1,1};
常量MPI_Aint位移[nitems]={offsetof(d_类型,时间戳),offsetof(d_类型,lightpostID),offsetof(d_类型,carTraffic)};
MPI_数据类型类型[nitems]={string_dt,MPI_INT,MPI_INT};
MPI类型创建结构(单位、区块长度、位移、类型和交通类型);
MPI类型提交(和流量类型);
MPI_屏障(MPI_通信世界);
if(rankProcess==MASTER\u秩){
工作进程=SizeCommunicationr-1;
string str=“11:55”;
d_型缓冲器;
buffer.timestamp=“11:00”;
buffer.lightpostID=1;
缓冲区。carTraffic=500;
消息标签=类主机;

对于(idProcess=1;idProcess我没有使用
MPI
,但它可以处理非POD和/或不可复制的类型的指示在哪里?您的
结构将
std::string
作为成员变量,从而使它们非POD和不可复制。我认为您需要使用
C
layout-compatible结构。不能使用
std::string
并将派生数据类型当作
char[6]来构建考虑使用BooSt.MPi传递C++数据类型。MPI本身只有对类一无所知的C API。<代码> STD::String 是一个包含堆指针的结构体。没有办法定义一个单MPI派生的数据类型,它与许多代码的实例<>代码:ST::String < /Cord> >,因为堆指针不同。每一个实例。