Skip to content

Instantly share code, notes, and snippets.

@ugovaretto
Created March 22, 2013 11:24
Show Gist options
  • Save ugovaretto/5220586 to your computer and use it in GitHub Desktop.
Save ugovaretto/5220586 to your computer and use it in GitHub Desktop.
Test copy constructor within CUDA kernel.
#include <iostream>
#include <cuda_runtime.h>
class CClass {
public:
class Inner {
public:
__host__ __device__ Inner() : value_( 5 ) {}
__host__ __device__ Inner( const Inner& i ) : value_( 9 * i.value_ ) {}
__host__ __device__ int value() const { return value_; }
private:
int value_;
};
public:
__host__ __device__ CClass() : value_( 1 ) {}
__host__ __device__ CClass( const CClass& cc ) : value_( 3 ) {}
__host__ __device__ int value() const { return value_; }
__host__ __device__ Inner inner() const { return Inner(); }
private:
int value_;
};
__global__ void test_copy_constructor_kernel( CClass cc, int* output ) {
CClass::Inner i1;
CClass::Inner i2 = cc.inner();
*output = i2.value();
}
void test_copy_constructor_cpu() {
CClass::Inner i1;
CClass cc;
CClass::Inner inner = cc.inner();
std::cout << "Test copy constructor cpu - " << inner.value() << std::endl;
}
void test_copy_constructor_cuda() {
int* cuda_out = 0;
cudaMalloc( &cuda_out, sizeof( int ) );
CClass cc;
test_copy_constructor_kernel<<< dim3(4,4),dim3(4,4,4) >>>( cc, cuda_out );
int out = 0;
cudaMemcpy( &out, cuda_out, sizeof( int ), cudaMemcpyDeviceToHost );
cudaFree( cuda_out );
std::cout << "Test copy constructor cuda - " << out << std::endl;
}
int main(int, char**) {
test_copy_constructor_cpu();
test_copy_constructor_cuda();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment