Created
May 7, 2012 09:19
-
-
Save ugovaretto/2626866 to your computer and use it in GitHub Desktop.
Minimal cuda program(memory transfer only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8) | |
project(cudamemcpy) | |
include(FindCUDA) | |
include_directories(${CUDA_INCLUDE_DIRS}) | |
set( SRCS cudamemcpy.cpp ) | |
add_executable( cudamemcpy ${SRCS} ) | |
target_link_libraries( cudamemcpy ${CUDA_LIBRARIES} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cuda_runtime.h> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
bool checkCudaStatus( cudaError_t status ) { | |
if( status != cudaSuccess ) { | |
std::cerr << cudaGetErrorString( status ) << std::endl; | |
return false; | |
} else return true; | |
} | |
// perform cpu->gpu->cpu copy | |
int main( int argc, char** argv ) { | |
const size_t ARRAY_SIZE = 0x10000; | |
const double INIT_VALUE_IN = 1.0; | |
const double INIT_VALUE_OUT = 0.0; | |
std::vector< double > host_array_in( ARRAY_SIZE, INIT_VALUE_IN ); | |
std::vector< double > host_array_out( ARRAY_SIZE, INIT_VALUE_OUT ); | |
double* dev_array = 0; | |
cudaError_t status = cudaMalloc( &dev_array, ARRAY_SIZE * sizeof( double ) ); | |
if( !checkCudaStatus( status ) ) return -1; | |
status = cudaMemcpy( dev_array, &host_array_in[ 0 ], ARRAY_SIZE * sizeof( double ), cudaMemcpyHostToDevice ); | |
if( !checkCudaStatus( status ) ) return -1; | |
status = cudaMemcpy( &host_array_out[ 0 ], dev_array, ARRAY_SIZE * sizeof( double ), cudaMemcpyDeviceToHost ); | |
if( !checkCudaStatus( status ) ) return -1; | |
std::cout << std::boolalpha | |
<< std::equal( host_array_in.begin(), host_array_in.end(), host_array_out.begin() ) | |
<< std::endl; | |
status = cudaFree( dev_array ); | |
if( !checkCudaStatus( status ) ) return -1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment