2015-03-10 3 views
0

Я новичок в CUDA и платформе тяги. Я просматривал множество примеров и вопросов, касающихся моей проблемы, однако я не смог передать решение.CUDA thrust :: device_vector класса | error

У меня есть класс Cell, который должен содержать вектор Tree (другой класс).

Это мой Cell.h

#pragma once 

#include <thrust/host_vector.h> 
#include <thrust/device_vector.h> 
#include <thrust/generate.h> 
#include <thrust/reduce.h> 
#include <thrust/functional.h> 




#include "Tree.h" 

class Cell { 
    int idx; 
    float xmid, ymid; 
    float dx, dy; 
    int nTrees; 
    thrust::host_vector<Tree> trees; 
// thrust::device_vector<Tree> trees;   <-- this is what I want 
public: 
    Cell(); 
    Cell(int, float, float, float, float, int); 
    void set(int, float, float, float, float, int); 
    void add(float, float, float); 
    void add(float); 
    void add(); 
    virtual ~Cell(); 
    void print(); 
    void copyToDev(); 
}; 

и вот мой Tree.h

#pragma once 
#include <iostream> 
#include <cstdlib> 
using namespace std; 



class Tree { 

    float x, y, r; 
    int idx; 
public: 
    Tree(); 
    Tree(float, float, float, int); 
    void set(float, float, float, int); 
    virtual ~Tree(); 
    void print(); 
}; 

Классы и реализованы в файле с расширением .cu. В моем main.cu я теперь хочу инициализировать Cell C. Учитывая вышеприведенный код, я могу скомпилировать код (используя Visual Studio 2013, который я раньше не использовал, так что это может быть другой проблемой для меня). Однако, если я не ошибаюсь, используя host_vector, я не использую свой GPU. Вместо этого я хочу использовать device_vector.

Но если я скомпилировать код с device_vector вместо host_vector я получаю следующую ошибку

1>------ Build started: Project: WTM, Configuration: Debug Win32 ------ 
1> Cell.cu 
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\detail\error_category.inl(102): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 
1>   c:\program files (x86)\microsoft visual studio 12.0\vc\include\string.h(168) : see declaration of 'strerror' 
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\bulk\detail\pointer_traits.hpp(55): warning C4800: 'unsigned int' : forcing value to bool 'true' or 'false' (performance warning) 
1>c:\users\thomas\documents\visual studio 2013\projects\wtm\wtm\cell.cu(40): warning C4018: '<' : signed/unsigned mismatch 
1>c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\system\cuda\detail\for_each.inl(84): error C2027: use of undefined type 'thrust::detail::STATIC_ASSERTION_FAILURE<false>' 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\for_each.inl(71) : see reference to function template instantiation 'RandomAccessIterator thrust::system::cuda::detail::for_each_n<thrust::system::cuda::detail::tag,InputIterator,Size,UnaryFunction>(thrust::system::cuda::detail::execution_policy<thrust::system::cuda::detail::tag> &,RandomAccessIterator,Size,UnaryFunction)' being compiled 
1>   with 
1>   [ 
1>    RandomAccessIterator=thrust::device_ptr<Tree> 
1> ,   InputIterator=thrust::device_ptr<Tree> 
1> ,   Size=int 
1> ,   UnaryFunction=thrust::detail::allocator_traits_detail::gozer 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\allocator\destroy_range.inl(136) : see reference to function template instantiation 'InputIterator thrust::for_each_n<DerivedPolicy,Pointer,Size,thrust::detail::allocator_traits_detail::gozer>(const thrust::detail::execution_policy_base<DerivedPolicy> &,InputIterator,Size,UnaryFunction)' being compiled 
1>   with 
1>   [ 
1>    InputIterator=thrust::device_ptr<Tree> 
1> ,   DerivedPolicy=thrust::system::cuda::detail::tag 
1> ,   Pointer=thrust::device_ptr<Tree> 
1> ,   Size=int 
1> ,   UnaryFunction=thrust::detail::allocator_traits_detail::gozer 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\allocator\destroy_range.inl(157) : see reference to function template instantiation 'void thrust::detail::allocator_traits_detail::destroy_range<Allocator,Pointer,Size>(Allocator &,Pointer,Size)' being compiled 
1>   with 
1>   [ 
1>    Allocator=thrust::device_malloc_allocator<Tree> 
1> ,   Pointer=thrust::device_ptr<Tree> 
1> ,   Size=int 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\contiguous_storage.inl(256) : see reference to function template instantiation 'void thrust::detail::destroy_range<thrust::device_malloc_allocator<T>,Base,int>(Allocator &,Pointer,Size)' being compiled 
1>   with 
1>   [ 
1>    T=Tree 
1> ,   Base=thrust::device_ptr<Tree> 
1> ,   Allocator=thrust::device_malloc_allocator<Tree> 
1> ,   Pointer=thrust::device_ptr<Tree> 
1> ,   Size=int 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\contiguous_storage.inl(255) : while compiling class template member function 'void thrust::detail::contiguous_storage<T,Alloc>::destroy(thrust::detail::normal_iterator<thrust::device_ptr<T>>,thrust::detail::normal_iterator<thrust::device_ptr<T>>)' 
1>   with 
1>   [ 
1>    T=Tree 
1> ,   Alloc=thrust::device_malloc_allocator<Tree> 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\vector_base.inl(474) : see reference to function template instantiation 'void thrust::detail::contiguous_storage<T,Alloc>::destroy(thrust::detail::normal_iterator<thrust::device_ptr<T>>,thrust::detail::normal_iterator<thrust::device_ptr<T>>)' being compiled 
1>   with 
1>   [ 
1>    T=Tree 
1> ,   Alloc=thrust::device_malloc_allocator<Tree> 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\detail\vector_base.inl(44) : while compiling class template member function 'thrust::detail::vector_base<T,Alloc>::vector_base(void)' 
1>   with 
1>   [ 
1>    T=Tree 
1> ,   Alloc=thrust::device_malloc_allocator<Tree> 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\device_vector.h(67) : see reference to function template instantiation 'thrust::detail::vector_base<T,Alloc>::vector_base(void)' being compiled 
1>   with 
1>   [ 
1>    T=Tree 
1> ,   Alloc=thrust::device_malloc_allocator<Tree> 
1>   ] 
1>   c:\program files\nvidia gpu computing toolkit\cuda\v7.0\include\thrust\device_vector.h(54) : see reference to class template instantiation 'thrust::detail::vector_base<T,Alloc>' being compiled 
1>   with 
1>   [ 
1>    T=Tree 
1> ,   Alloc=thrust::device_malloc_allocator<Tree> 
1>   ] 
1>   c:\users\thomas\documents\visual studio 2013\projects\wtm\wtm\cell.h(27) : see reference to class template instantiation 'thrust::device_vector<Tree,thrust::device_malloc_allocator<T>>' being compiled 
1>   with 
1>   [ 
1>    T=Tree 
1>   ] 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

Я действительно не знаю, как подойти к этому. Может быть, кто-то может провести меня через эту проблему. Заранее спасибо!

EDIT

Как было отмечено, я забыл main, это ниже код, Асо в .cu файле

#include "Cell.h" 


int main(void) 
{ 


    Cell D(0, 0.5, 0.5, 1, 1); 

    D.print(); 
    return 0; 
} 

снижена до минимальной части. Если дополнительная информация отсутствует, просто дайте мне знать.

+0

Вы создаете устройство_vector из Cell? Вам необходимо предоставить MCVE. Вы запрашиваете помощь при ошибке компиляции, и вы даже не указали строку кода, которая порождает ошибку. –

ответ

2

Если посмотреть в файл с определением thrust::system::cuda::detail::for_each_n как в сообщении об ошибке предполагая, вы найдете следующий комментарий (link to github):

// we're attempting to launch a kernel, assert we're compiling with nvcc 
// ======================================================================== 
// X Note to the user: If you've found this line due to a compiler error, X 
// X you need to compile your code using nvcc, rather than g++ or cl.exe X 
// ======================================================================== 

Убедитесь, что вы действительно используете nvcc скомпилировать программу , а не просто попытка включить в обычный проект c++ проект, который скомпилируется с визуальной студией cl.

+0

Не могли бы вы рассказать, как это сделать в Visual Studio Nsight? – k1next

+0

Оказалось, что это была проблема. Однако теперь я могу только скомпилировать его в linux через командную строку, но не в VS, поскольку я не знаю, как настроить VS для использования nvcc. – k1next