1

一部の CPU ルーチンが、GPU を使用して一部の計算を高速化する関数を呼び出せるようにするコードをコンパイルしようとしています。GPU コードは Thrust、具体的には reduce と device_ptr を使用します。nvcc を使用してスタンドアロンとして GPU コードをビルドすると、問題は発生しません。ただし、GPU コードを CPU C++ コードと統合しようとすると、最終的な「ラッパー」をコンパイルするときに次のコンパイラ エラーが発生します。

nvcc -O2 -c NLC_2D_TFIM.cpp -lcuda -lcudart -lcublas -lcusparse -L../CUDA/Lanczos/sort/sort/gnu/release -lmgpusort 
In file included from /usr/local/cuda/bin/../include/thrust/pair.h:265:0,
                 from /usr/local/cuda/bin/../include/thrust/tuple.h:35,
                 from /usr/local/cuda/bin/../include/thrust/detail/functional/actor.h:29,
                 from /usr/local/cuda/bin/../include/thrust/detail/functional/placeholder.h:20,
                 from /usr/local/cuda/bin/../include/thrust/functional.h:26,
                 from /usr/local/cuda/bin/../include/thrust/system/detail/error_category.inl:22,
                 from /usr/local/cuda/bin/../include/thrust/system/error_code.h:516,
                 from /usr/local/cuda/bin/../include/thrust/system/cuda_error.h:26,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/cuda/malloc.inl:26,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/cuda/malloc.h:50,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/dispatch/malloc.h:22,
                 from /usr/local/cuda/bin/../include/thrust/detail/device_malloc.inl:23,
                 from /usr/local/cuda/bin/../include/thrust/device_malloc.h:102,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/internal_allocator.h:22,
                 from /usr/local/cuda/bin/../include/thrust/detail/uninitialized_array.h:23,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/cuda/copy_cross_space.inl:20,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/cuda/copy_cross_space.h:57,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/cuda/dispatch/copy.h:23,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/cuda/copy.h:21,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/dispatch/copy.h:24,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/copy.inl:20,
                 from /usr/local/cuda/bin/../include/thrust/detail/backend/copy.h:44,
                 from /usr/local/cuda/bin/../include/thrust/detail/copy.inl:20,
                 from /usr/local/cuda/bin/../include/thrust/detail/copy.h:39,
                 from /usr/local/cuda/bin/../include/thrust/detail/reference_base.inl:18,
                 from /usr/local/cuda/bin/../include/thrust/detail/reference_base.h:138,
                 from /usr/local/cuda/bin/../include/thrust/device_reference.h:27,
                 from /usr/local/cuda/bin/../include/thrust/detail/device_ptr.inl:23,
                 from /usr/local/cuda/bin/../include/thrust/device_ptr.h:181,
                 from ../CUDA/Lanczos/hamiltonian.h:32,
                 from ../CUDA/Lanczos/lanczos.h:8,
                 from NLC_2D_TFIM.cpp:17:
/usr/local/cuda/bin/../include/thrust/detail/pair.inl: In function ‘bool thrust::operator<(const thrust::pair<T1, T2>&, const thrust::pair<T1, T2>&)’:
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:22: error: ‘.’ cannot appear in a constant-expression
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:46: error: ‘.’ cannot appear in a constant-expression
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:36: error: parse error in template argument list
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:36: error: ‘.’ cannot appear in a constant-expression
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:58: error: ‘.’ cannot appear in a constant-expression
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:69: error: ‘.’ cannot appear in a constant-expression
/usr/local/cuda/bin/../include/thrust/detail/pair.inl:72:12: error: parse error in template argument list
make: *** [NLC_2D_TFIM.o] Error 1

NLC_2D_TFIMs を使用する別のモジュール ( Graphs) で動作しますstd::pairが、これらのいずれも GPU と対話するモジュールに渡されません。すべてのヘッダーはstd、ではなく名前空間として使用しますthrust。GPU ハンドラーに渡すすべてのパラメーターは、通常の C 配列、ints などです。

上記の行は次のとおりです。

#include"lanczos.h"

使用するもの:

#include"hamiltonian.h"

そしてそこから:

#include<thrust/device_ptr.h>

NLC_2D_TFIM.cuラッパー」のコードは次のとおりです。

ReadGraphsFromFile(fileGraphs, "rectanglegraphs.dat", TypeFlag); //graphs the information generated by the Graphs module

    double J=1.;

    for(int hh=1; hh<10; hh++) {
        h = hh;

        //Create some storage for things to be used in GPU functions
        d_hamiltonian* HamilLancz = (d_hamiltonian*)malloc(HowMany*sizeof(d_hamiltonian));
        parameters* data = (parameters*)malloc(HowMany*sizeof(parameters));
        double** groundstates = (double**)malloc(HowMany*sizeof(double*));
        double** eigenvalues = (double**)malloc(HowMany*sizeof(double*));
        int* NumElem = (int*)malloc(HowMany*sizeof(int));
        int** Bonds = (int**)malloc(HowMany*sizeof(int*));

        //Go through each graph we read in earlier
        unsigned int i = 1;
        while ( i<fileGraphs.size() && fileGraphs.at(i)->Order < 14) { //skip the zeroth graph
            //CPU gets the energy for smaller graphs
            GENHAM HV(fileGraphs.at(i)->Order, J, h, fileGraphs.at(i)->AdjacencyList, TypeFlag);
            LANCZOS lancz(HV.Vdim);  //dimension of reduced Hilbert space (Sz sector)
            HV.SparseHamJQ();  //generates sparse matrix Hamiltonian for Lanczos
            energy = lancz.Diag(HV, 1, prm.valvec_, eVec);
            i++;
        }

        if( argv[0] == "--gpu" || argv[0] == "-g" )
        {
            while ( i < fileGraphs.size() )
            {
                i += 30;

                for( int j = 0; j < HowMany; j++)
                {

                    Bonds[ j ] = (int*)malloc(sizeof(int)*3*fileGraphs.at(i - j)->Order);
                    for(unsigned int k = 0; k < fileGraphs.at(i - j)->Order; k++)
                    {
                        Bonds[ j ][ k ] = k;
                        Bonds[ j ][ k + fileGraphs.at(i - j)->Order ] = fileGraphs.at(i - j)->AdjacencyList.at(2*k).second;
                        Bonds[ j ][ k + 2*fileGraphs.at(i - j)->Order ] = fileGraphs.at(i - j)->AdjacencyList.at(2*k + 1).second;
                    }

                    data[ j ].Sz = 0;
                    data[ j ].dimension = 2;
                    data[ j ].J1 = J;
                    data[ j ].J2 = h;
                    data[ j ].modelType = 2;
                    eigenvalues[ j ] = (double*)malloc(sizeof(double));

                }
                //Calls the CPU functions which will talk to the GPU, including Thrust
                ConstructSparseMatrix(HowMany, Bonds, HamilLancz, data, NumElem, 1);
                lanczos(HowMany, NumElem, HamilLancz, groundstates, eigenvalues, 200, 1, 1e-12);

したがってstd::pair、GPU に渡される には何もありません。スラスト呼び出しは次のとおりです。

for(int i = 0; i < howMany; i++)
{
    thrust::device_ptr<int> red_ptr(d_H[i].set);
    numElem[i] = thrust::reduce(red_ptr, red_ptr + rawSize[i]);
}
4

2 に答える 2

1

問題は、 Blitzを使用してコードに対してリンクしていたことであることが判明しました。すべての Blitz データ構造とそのための include ステートメントを削除すると、コンパイルの問題が解決されました。Blitz は独自の名前空間を使用するため、おそらく何かが推力と競合しているか、どこかに } または > がありません。

于 2012-07-17T16:07:56.047 に答える
1

これが正しい答えかどうかはわかりませんが、ファイル拡張子が cpp の場合、nvcc はそれを通常の C++ コンパイラに渡すだけですか? ファイルの名前を .cu に変更するとどうなりますか?

(また-c、同じコンパイル コマンドですべてのライブラリを使用する必要があるかどうかも-cわかりません。通常、リンクは行わないことをお勧めします。)

于 2012-07-12T19:40:52.393 に答える