gpt4 book ai didi

c - BFS 顺序算法中的段错误

转载 作者:行者123 更新时间:2023-11-30 14:27:20 31 4
gpt4 key购买 nike

我已经制作了 BFS 算法的并行版本,现在我正在尝试序列化相同的算法以了解加速情况。我的代码是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct Node
{
int begin; // begining of the substring
int num; // size of the sub-string
};

void BFS (Node *Va, int *Ea, bool* Fa, bool *Xa, int *Ca,bool *para, int tid)
{

int nid;

if (Fa[tid] == true && Xa[tid] == false)
{
Fa[tid] = false;

for (int i = Va[tid].begin; i < (Va[tid].begin + Va[tid].num); i++) // Va begin is where it's edges' subarray begins, Va is it's number of elements
{
nid = Ea[i];

if (Xa[nid] == false)
{
Ca[nid] = Ca[tid] + 1;
Fa[nid] = true;
*para = true;
}
}
Xa[tid] = true;
}

}

int main()
{


struct Node Va[4];
Va[0].begin=0;
Va[0].num=2;
Va[1].begin=1;
Va[1].num=0;
Va[2].begin=2;
Va[2].num=2;
Va[3].begin=1;
Va[3].num=0;
int edges[]={1,2,3,1};
//cudaMalloc((void**)&Va,sizeof(Node)*4);
//cudaMemcpy(Va,node,sizeof(Node)*4,cudaMemcpyHostToDevice);

int Ea[4]={1,2,3,3};
//cudaMalloc((void**)&Ea,sizeof(Node)*4);
//cudaMemcpy(Ea,edges,sizeof(Node)*4,cudaMemcpyHostToDevice);

bool Fa[4]={false};
int source=0;
Fa[source]=true;
//cudaMalloc((void**)&Fa,sizeof(bool)*4);
//cudaMemcpy(Fa,frontier,sizeof(bool)*4,cudaMemcpyHostToDevice);

bool Xa[4]={false};
//cudaMalloc((void**)&Xa,sizeof(bool)*4);
//cudaMemcpy(Xa,visited,sizeof(bool)*4,cudaMemcpyHostToDevice);

int Ca[4]={0};
//cudaMalloc((void**)&Ca,sizeof(int)*4);
//cudaMemcpy(Ca,custo,sizeof(int)*4,cudaMemcpyHostToDevice);

//dim3 grid(1,1,1);
//dim3 threads(4,1,1);


bool* para;
int i=1,n=0,j;


for (j=0; j<4 ; j++)
{
BFS(Va,Ea,Fa,Xa,Ca,para,j);

printf("Run number: %d >> ",n);
for(int i=0;i<4;i++)
printf("%d ",Ca[i]);
printf("\n");


printf ("\n Xa :");
for(int i=0;i<4;i++)
printf("%d ",Xa[i]);
printf("\n\n");



n++;
printf("%d",n);
}



printf("\nFinal:\n");
for(int i=0;i<4;i++)
printf("%d ",Ca[i]);
printf("\n");

}

请注意,它以前是 CUDA 程序...

此外,我正在使用 g++ 进行编译,因此我可以使用 bool 变量。

问题是:当我第一次运行 BFS 函数时,我就遇到了段错误。

最佳答案

para 没有指向任何东西,但你却给它赋值了。

将其声明为 bool para 并传递

关于c - BFS 顺序算法中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7965752/

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