Kejie's profile◆ Neverland ◆PhotosBlogListsMore Tools Help
    8/20/2009

    CUDA Eclipse project setup step by step

    UPDATE: I still have problems for step i), executable file produced, and can be run normally. 
    Only problem now is debug step. I need some advices!



    a) Start Eclipse and set the workspace to the SDK install folder/projects

    b) Create a new C or C++ Makefile Project
    New>File>C++ Project; then choose ("Makefile project" in the "Project types" selection window)
    In the "Toolchain" selection window select "Linux GCC",set a name and click Finish. Let's say the project name is CUDA_test

    c) Add a new Source File with the extension eg. "CUDA_test.cu" and click Finish
    File>New>Source File

    d) Add a new File called "Makefile" (File>New>File) and click ok

    e) Edit the Makefile
    Copy these lines to the makefile:

    #################################################

    # Build script for project

    #################################################


    EXECUTABLE := CUDA_test

    # cu files

    CUFILES := CUDA_test.cu

    # c/c++

    CCFILES :=


    CUDA_INSTALL_PATH := /usr/local/cuda

    PROJECT_PATH := .

    NV_ROOT_PATH := /home/username/NVIDIA_GPU_Computing_SDK/C


    ifeq ($(emu), 1)

    LIB := -lcufftemu

    else

    LIB := -lcufft

    endif


    # Basic directory setup for SDK

    # (override directories only if they are not already defined)

    #SRCDIR ?= $(PROJECT_PATH)/Source

    #ROOTDIR ?= $(PROJECT_PATH)

    #ROOTBINDIR ?= $(PROJECT_PATH)/bin

    #BINDIR ?= $(ROOTBINDIR)/linux

    #ROOTOBJDIR ?= $(ROOTBINDIR)/obj

    #LIBDIR := $(NV_ROOT_PATH)/lib

    #COMMONDIR := $(NV_ROOT_PATH)/common


    include ../../common/common.mk

    #######################################################

    f) now edit the CUDA_test.cu file 
    ##############################################################################
    /*
    * Copyright 1993-2008 NVIDIA Corporation. All rights reserved.
    *
    * NOTICE TO USER:
    *
    * This source code is subject to NVIDIA ownership rights under U.S. and
    * international Copyright laws. Users and possessors of this source code
    * are hereby granted a nonexclusive, royalty-free license to use this code
    * in individual and commercial software.
    *
    * NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
    * CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
    * IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH
    * REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
    * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
    * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
    * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
    * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
    * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
    * OR PERFORMANCE OF THIS SOURCE CODE.
    *
    * U.S. Government End Users. This source code is a "commercial item" as
    * that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting of
    * "commercial computer software" and "commercial computer software
    * documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995)
    * and is provided to the U.S. Government only as a commercial end item.
    * Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
    * 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
    * source code with only those rights set forth herein.
    *
    * Any use of this source code in individual and commercial software must
    * include, in the user documentation and internal comments to the code,
    * the above Disclaimer and U.S. Government End Users Notice.
    */


    // includes, system
    #include <stdio.h>
    #include <assert.h>

    // Simple utility function to check for CUDA runtime errors
    void checkCUDAError(const char* msg);

    // Part 1 of 1: implement the kernel
    __global__ void reverseArrayBlock(int* d_B, int* d_A )
    {
    int tx = threadIdx.x;
    d_B[blockDim.x-tx-1] = d_A[tx];
    }

    ////////////////////////////////////////////////////////////////////////////////
    // Program main
    ////////////////////////////////////////////////////////////////////////////////
    int main( int argc, char** argv)
    {
    // pointer for host memory and size
    int *h_a;
    int dimA = 256;

    // pointer for device memory
    int *d_b, *d_a;

    // define grid and block size
    int numBlocks = 1;
    int numThreadsPerBlock = dimA;

    // allocate host and device memory
    size_t memSize = numBlocks * numThreadsPerBlock * sizeof(int);
    h_a = (int *) malloc(memSize);
    cudaMalloc( (void **) &d_a, memSize );
    cudaMalloc( (void **) &d_b, memSize );

    // Initialize input array on host
    for (int i = 0; i < dimA; ++i)
    {
    h_a[i] = i;
    }

    // Copy host array to device array
    cudaMemcpy( d_a, h_a, memSize, cudaMemcpyHostToDevice );

    // launch kernel
    dim3 dimGrid(numBlocks);
    dim3 dimBlock(numThreadsPerBlock);
    reverseArrayBlock<<< dimGrid, dimBlock >>>( d_b, d_a );

    // block until the device has completed
    cudaThreadSynchronize();

    // check if kernel execution generated an error
    // Check for any CUDA errors
    checkCUDAError("kernel invocation");

    // device to host copy
    cudaMemcpy( h_a, d_b, memSize, cudaMemcpyDeviceToHost );

    // Check for any CUDA errors
    checkCUDAError("memcpy");

    // verify the data returned to the host is correct
    for (int i = 0; i < dimA; i++)
    {
    assert(h_a[i] == dimA - 1 - i );
    }

    // free device memory
    cudaFree(d_a);
    cudaFree(d_b);

    // free host memory
    free(h_a);

    // If the program makes it this far, then the results are correct and
    // there are no run-time errors. Good work!
    printf("Correct!\n");

    return 0;
    }

    void checkCUDAError(const char *msg)
    {
    cudaError_t err = cudaGetLastError();
    if( cudaSuccess != err)
    {
    fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
    exit(EXIT_FAILURE);
    }
    }

    #######################################################
    g) now you have to change the Project properties.
    Right click the project and select properties.
    g.1) Select C/C++ Build options.
    Deselect the option "Use default build command".
    Then, on the Build command input field enter:
    For release mode let the field with "make" unchanged.
    For debug mode enter "make dbg=1" (Use customarily this option)
    For debug emulation mode enter "make dbg=1 emu=1"
    (this enables the user to acces GPU functions when debugging
    Notice that that the used compiler for GPU functions is not nvcc but gcc)
    For emulation mode enter "make emu=1".
    	g.2) Still in the C/C++ Build options, 
    Switch to tab "Behaviour"
    Empty the textboxes next to Build (Incremental build) and Clean [delete "all" and "clean"]
    (located at the bottom of :Workbench Build Type" area)
    Select the line "Discovery options" under the menu C/C++ Build
    set the Discovery profile to "GCC per project scanner info profile"
    g.3) At C/C++ General
    Select the line "Paths and symbols" under the menu and
    switch to the tab "Library Paths". Click on Add and enter "$LD_LIBRARY_PATH".
    Select the box "Add to all configurations"
    At last click ok to close this tab, and again to close the "Paths and symbols window"
    g.4) Ctrl + b to Build Project

    h) Now create a debug profile for this project (assuming the executable could be produced).
    h.1) Right click the Project option and select "Debug As..">"Open Debug Dialog...".
    Double left-click on C/C++ Local Application to add a new debug configuration

    h.2) Select the new configuration and then in the line "C/C++ Application:"
    click "Browse..." and select the executable from your filesystem.
    [it should be here "/home/username/NVIDIA_GPU_Computing_SDK/C/bin/linux/release/"]

    h.3) Now switch to the tab common and select in the box "Display in favorites menu" the
    checkbox for Debug. Press Apply and close the window.

    i) Press F11 to start the program or select the start configuration from the "bug" button
    in the shortcut menu.
    Then you have the following options:

    RUN dialog give the different opportunities
    F8 from break point to break point (or end)
    F6 execute stepwise
    F5 step into function


    Thanks bin04017 @ gmail.com for his/her instructions from nvidia forum.

    Comments (4)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Kejie Liwrote:
    才发现你也在我的facebook friends里面,啥子时候悄悄咪咪加的哦,居然我都不晓得。。。汗啊!
    Aug. 26
    Shuhao Caowrote:
    765-543-8558 学长把电话交出来三!
    Aug. 25
    Kejie Liwrote:
    原来是师弟啊!居然也在purdue??? 咋个没有见过??!!!
    Aug. 25
    Shuhao Caowrote:
    nice, thanks
    Aug. 21

    Trackbacks

    The trackback URL for this entry is:
    http://likejie.spaces.live.com/blog/cns!57B55FD721D13595!2156.trak
    Weblogs that reference this entry
    • None