2017-09-27 43 views
0

我有一个程序,包括3个文件,一个.c文件和两个.cu文件,nn.cuparallel.cu。主要功能位于一个.cu文件中,nn.cu.c文件(utils.c)我在parallel.cu中将其作为extern "C"。我想进一步并行化程序(运行完全没有cilk),所以我认为cilk,与_Cilk_spawn_Cilk_syncCilk和CUDA的组合和汇编

int main(int argc, char* argv[]) { 

    clock_t begin = clock(); 

    srand((unsigned)time(NULL)); 

    int n_inputs = atoi(argv[2]); 
    int n_hidden = atoi(argv[3]); 
    int n_outputs = atoi(argv[4]); 

    // Build output layer 
    NeuralNet nn = buildNeuralNet(n_inputs, n_outputs, n_hidden); 

    // Build training samples 
    int _p1[] = {0,0}; 
    Pattern p1 = makePatternSingleOutput(_p1, 0); 
    int _p2[] = {0,1}; 
    Pattern p2 = makePatternSingleOutput(_p2, 1); 
    int _p3[] = {1,1}; 
    Pattern p3 = makePatternSingleOutput(_p3, 1); 
    int _p4[] = {1,0}; 
    Pattern p4 = makePatternSingleOutput(_p4, 1); 

    Pattern patterns[] = {p3, p2, p1, p4}; 

    // Train the network 
    _Cilk_spawn train_network(patterns, 4, atoi(argv[1]), nn); 

    printf("\n\nTesting the network\n"); 

    _Cilk_sync; 

    _Cilk_spawn update_pattern(p2, nn); 
    for (int i=0; i < nn.n_outputs; i++) { 
     printf("Output: %f, expected: %i\n", nn.out_output[i], p2.result[i]); 
     printf("NN Error : %f\n", 1.0f - nn.out_output[i]); 
    } 
    cudaDeviceReset(); 

    _Cilk_sync; 

    clock_t end = clock(); 
    double time_spent = (double)(end - begin)/CLOCKS_PER_SEC; 
    printf("Runtime : %f\n", time_spent); 

    return 0; 

} 

问题是,当我尝试使用nvcc一起编译所有这些:

$ nvcc -Wno-deprecated-gpu-targets -o my_nn_cilk nn.cu parallel.cu -lm 
nn.cu(241): error: identifier "_Cilk_spawn" is undefined 

nn.cu(241): error: expected a ")" 

nn.cu(245): error: identifier "_Cilk_sync" is undefined 

nn.cu(247): error: identifier "_Cilk_spawn" is undefined 

nn.cu(247): error: expected a ")" 

5 errors detected in the compilation of "/tmp/tmpxft_00003b52_00000000-14_nn.cpp1.ii". 

这两个函数我_Cilk_spawn调用所需的CUDA内核。 即使我添加到nvcc命令参数-lcilkrts,错误也是一样的。 另外,我有#include "cilk/cilk.h"在代码的开头。

你能帮我吗?为什么它会显示这些错误并且不会编译? 提前谢谢!

回答

1

不编译的原因是,nvcc不支持cilk实现和关键字。你需要一个从你的cilk代码调用CUDA函数的包装器。以下是关于如何编写包装并从您的cilk代码调用它的示例:cilk with cuda sample

在链接中,还解释了如何编译cuda代码和cilk代码以及如何链接它们。

+0

我会试试这个,谢谢! – BabisI