Linux CUDA C MPI generates dynamic link libraries __linux

Source: Internet
Author: User
Tags nvcc

In recent days want to c,cuda,mpi mixed compiled Linux to rewrite the dynamic link library libtest.so, after two or three days of the first large variety of search information, turn over a variety of makefile files, all kinds of reading blog, finally. Finally, I'm crying for joy.

1. First understand how the CPU side to encapsulate code into a dynamic link library

Reprint Address: http://www.cnblogs.com/huangxinzhen/p/4047051.html

Of course, a lot of relevant online, you can refer to other blogs, as long as the correct results can be out.

1, the compilation of dynamic library

The following is an example of how to generate a dynamic library. Here is a header file: So_test.h, three. c Files: test_a.c, TEST_B.C,

TEST_C.C, we compile these several files into a dynamic library: libtest.so.

//so_test.h:
#include "stdio.h"
Void Test_a ();
void Test_b ();
void Test_c ();

//test_a.c:
#include "so_test.h"
Void Test_a ()
{
  printf ("This was in test_a...\n");
}


//TEST_B.C:
#include "so_test.h"
Void Test_b ()
{
  printf ("This was in test_b...\n ");
}



//test_c.c:
#include "so_test.h"
Void Test_c ()
{
  printf ("This was in Test_c...\n ");
}
Compiles these several files into a dynamic library: libtest.so
$ gcc test_a.c test_b.c test_c.c-fpic-shared-o libtest.so

2, links to dynamic libraries
In 1, we have successfully generated our own dynamic link library libtest.so, we use a program to invoke the function in this library.

The source file for the program is: test.c.

TEST.C:
#include "So_test.h"
int main ()
{
Test_a ();
Test_b ();
Test_c ();
return 0;
}
Deliver test.c and dynamic library libtest.so to execution file test:
$ gcc test.c-l.-ltest-o Test
Test whether the dynamic connection, if listed libtest.so, then should be connected to the normal
$ LDD Test
To execute test, you can see how it invokes functions in the dynamic library.
3, compile parameter analysis
The main thing is an option for the GCC command line:
-shared This option specifies that the build dynamic connection library (which allows the connector to generate an export symbol table of type T, and sometimes a weakly connected W-type export symbol),

External programs cannot connect without this flag. Equivalent to an executable file

-fpic: For code that is compiled as location independent, the compiled code is location-dependent without this option, so dynamic load is copied by code

Way to meet the needs of different processes, but not the purpose of real code segment sharing.

-L.: Indicates the library to connect to in the current directory

-ltest: When the compiler looks for a dynamic connection library, there is an implied naming rule, that is, precede the given name with Lib, followed by. So to determine the name of the library

Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.

Of course, if you have root permissions, you can modify the/etc/ld.so.conf file and then call/sbin/ldconfig to achieve the same purpose, but if

Without root permissions, you can only use the output Ld_library_path method.


2. C cuda mixed compilation, generate dynamic link library

After a hard search here, finally found the relevant test code on the Internet

Reprint Address: http://stackoverflow.com/questions/17278932/ Cuda-shared-library-linking-undefined-reference-to-cudaregisterlinkedbinary

Here to paste the code written by the blogger, very simple, easy to read

Here's a example Linux shared object creation along the lines you indicated:create a shared library containing I CUDA K Ernels that has a cuda-free wrapper/header. Create a test executable for the shared library.

The shared library. The build commands to this are as follows:

Nvcc-arch=sm_20-xcompiler '-fpic '-dc test1.cu test2.cu nvcc-arch=sm_20-xcompiler '-fpic '-dlink test1.o
- o link.o
g++-shared-o test.so test1.o test2.o Link.o-l/usr/local/cuda/lib64-lcudart

It seems you are missing the second step above in your makefile, but I Haven ' t analyzed if there are no other issues W ITH your makefile.

Now, for the test executable, the build commands are as follows:

g++-C main.cpp
g++-o testmain main.o test.so

To run it, simply execute the Testmain executable, but being sure the test.so library is on your ld_library_path.

These are the files I used for test purposes:

Test1.h:

int my_test_func1 ();

TEST1.CU:

#include <stdio.h>
#include "test1.h"

#define DSIZE 1024
#define DVAL
#define NTPB 256

# Define Cudacheckerrors (msg) \ do
    {\
        

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.