[Reprint] Cuda study Note 2

Source: Internet
Author: User
Cuda file organization

Original article address:Cuda Study Notes 2 Author:Ye Isaac

Cuda file organization:

1. Cuda projects can contain. Cu AND. cpp.

2. In the. Cu file, you can use # include "cuda_x.cuh" to call the functions in. Cu or # include "cpp_x.h ".

For example, declare Class A in test1.h; Define the related member functions of Class A in test1.cpp. In cuda_xx.cu, first # include "test1.h", and then define extern "C" Void Foo (...) {a aa; AA. memberfunc (...);} yes, and extern "C" is defined. call the foo function in the CPP file. If extern "C" is not available, an error is returned: "The external symbol _ foo that cannot be parsed is referenced in function _ main ".

The _ global _ function defined in other. Cus can be placed in the. cuh file and called by other. Cus. However, if it is not a function form provided by Cuda (such as a function in the C language), you must use extern "C ". In this case, do not. the cuh file declares that. definition in the Cu file, while other. when calling the Cu file, use extern "C" int Foo (...) before calling it (......); declare it. The function that calls Foo (for example, foo2) also needs to be modified using extern "C.

3. In the. cpp file, you can use the function declared as extern "c" in the. Cu file. Before calling the. cpp file, use the extern "C" + function declaration to declare it again. In. Cu, the extern "C" must be used for function definition. When calling. cpp, you also need to use extern "c" to modify it. However. in CPP, the _ global _ function cannot be called directly, because the compiler cannot parse symbols <...> and blockidx and threadidx, so the _ global _ function can only be placed in. define and call the Cu function.

4. Is it a little troublesome? Haha.Reason for modifying with extern "C", Ipv.cuis the extension c, but only cppis. The definition is C-like language compilation (nvcc.exe compilation ).

5. Check the SDK example. For example, in cuda1.cu, you can call the _ global _ function defined in cuda2.cu. However, after a long time, I reported the following error:

1> link...
1> cuda2.cu. OBJ: Error lnk2005: ___ device_stub _ z14colssumcuda232pfs_ii has been defined in cuda1.cu. OBJ
1> cuda2.cu. OBJ: Error lnk2005: "Void _ cdecl colssumcuda1__ _ entry (float *, float *, Int, INT )"(? Colssumcuda1__ entry @ yaxpam0hh @ Z) has been defined in cuda1.cu. OBJ
1> http://www.cnblogs.com/bin/win32/Debug/deviceQuery.exe: Fatal error lnk1169: locate one or more symbols with multiple definitions

After checking for half a day, we found that the original cuda2.cu (which defines the GPU's kernel function) needs to set cuda2.cu in [attribute] To "exclude from generation ".

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.