2017-12-18 219 views
2

我有一個由wrap.py生成的CPP程序。 wrap.py用於爲MPI程序生成包裝。它將任何正常的MPI呼叫重定向到PMPI呼叫用於攔截目的,以便例如性能分析。請下載生成的代碼here。我使用otf2來跟蹤MPI程序。PMPI和otf2:在CPP程序中鏈接C代碼

要解釋代碼:

// test4.cpp 
__attribute__((constructor)) void init(void) 
{ 
    if(!is_init) 
    { 
    archive = OTF2_Archive_Open("./", 
           "ArchiveTest", 
           OTF2_FILEMODE_WRITE, 
           1024 * 1024 /* event chunk size */, 
           4 * 1024 * 1024 /* def chunk size */, 
           OTF2_SUBSTRATE_POSIX, 
           OTF2_COMPRESSION_NONE); 
    is_init = true; 
    } 
} 

__attribute__((destructor)) void fini(void) 
{ 
    if(is_init) 
    { 
    OTF2_Archive_Close(archive); 
    is_init = false; 
    } 
} 

我要編譯代碼爲.so文件。所以當它被導入時,constructor會被調用;當.so被分離時,destructor被調用。

據otf2 here官方文檔,我編譯程序:

mpic++ -fpic -c `otf2-config --cflags` -o test4.o test4.cpp 
mpic++ -shared -o libtest4.so `otf2-config --ldflags` `otf2-config --libs` test4.o 

如果要擴展上面的命令行,你會得到:

mpic++ -fpic -c -I/usr/include -o test4.o test4.cpp 
mpic++ -shared -o libtest4.so -L/usr/lib -lotf2 -lm test4.o 

被攔截的MPI程序是從here

待辦事項攔截:

$ mpirun -n 2 -x LD_PRELOAD=./libtest4.so ./send_recv 
./send_recv: symbol lookup error: ./libtest4.so: undefined symbol: OTF2_Archive_Open 
./send_recv: symbol lookup error: ./libtest4.so: undefined symbol: OTF2_Archive_Open 
------------------------------------------------------- 
Primary job terminated normally, but 1 process returned 
a non-zero exit code.. Per user-direction, the job has been aborted. 
------------------------------------------------------- 
-------------------------------------------------------------------------- 
mpirun detected that one or more processes exited with non-zero status, thus causing 
the job to be terminated. The first process to do so was: 

    Process name: [[20246,1],0] 
    Exit code: 127 
-------------------------------------------------------------------------- 

所以看起來混合C和CPP引起的問題。鏈接器無法正確生成符號爲OTF2_Archive_OpenOTF2_Archive_Close的C函數。

我加2頁的聲明,告訴連接這些都是C函數(下載修改後的程序here):

_EXTERN_C_ OTF2_Archive* OTF2_Archive_Open (const char * archivePath, 
const char * archiveName, 
const OTF2_FileMode fileMode, 
const uint64_t chunkSizeEvents, 
const uint64_t chunkSizeDefs, 
const OTF2_FileSubstrate fileSubstrate, 
const OTF2_Compression compression 
); 
_EXTERN_C_ OTF2_ErrorCode OTF2_Archive_Close (OTF2_Archive * archive); 

但上方停留的問題。和建議?

UPDATE1: OTF2提供.a文件,而不是.so文件。

$ nm /usr/lib/libotf2.a| grep -i OTF2_Archive_Open 
       U otf2_archive_open 
0000000000000000 T OTF2_Archive_Open 
       U otf2_archive_open_def_files 
00000000000032e0 T OTF2_Archive_OpenDefFiles 
       U otf2_archive_open_evt_files 
00000000000030e0 T OTF2_Archive_OpenEvtFiles 
       U otf2_archive_open_snap_files 
00000000000034e0 T OTF2_Archive_OpenSnapFiles 
       U OTF2_Archive_Open 
0000000000001180 T otf2_archive_open 
0000000000005a40 T otf2_archive_open_def_files 
       U OTF2_Archive_OpenDefFiles 
0000000000005880 T otf2_archive_open_evt_files 
       U OTF2_Archive_OpenEvtFiles 
0000000000005c00 T otf2_archive_open_snap_files 
       U OTF2_Archive_OpenSnapFiles 


$ ldd ./libtest4.so 
    linux-vdso.so.1 => (0x00007ffe3a6ce000) 
    libmpi_cxx.so.1 => /usr/lib/libmpi_cxx.so.1 (0x00007f4757d67000) 
    libmpi.so.12 => /usr/lib/libmpi.so.12 (0x00007f4757a91000) 
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f475770e000) 
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f47574f8000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f475712e000) 
    libibverbs.so.1 => /usr/lib/libibverbs.so.1 (0x00007f4756f1e000) 
    libopen-rte.so.12 => /usr/lib/libopen-rte.so.12 (0x00007f4756ca4000) 
    libopen-pal.so.13 => /usr/lib/libopen-pal.so.13 (0x00007f4756a07000) 
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f47567e9000) 
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f47564e0000) 
    /lib64/ld-linux-x86-64.so.2 (0x00005620bef03000) 
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f47562dc000) 
    libhwloc.so.5 => /usr/lib/x86_64-linux-gnu/libhwloc.so.5 (0x00007f47560a1000) 
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f4755e99000) 
    libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f4755c96000) 
    libnuma.so.1 => /usr/lib/x86_64-linux-gnu/libnuma.so.1 (0x00007f4755a8a000) 
    libltdl.so.7 => /usr/lib/x86_64-linux-gnu/libltdl.so.7 (0x00007f4755880000) 



$ nm ./libtest4.so | grep -i OTF2_Archive_Open 
       U OTF2_Archive_Open 

奇怪的是,我沒有看到任何libotf2.aldd輸出。但是,如果你從他們的網站上試用otf2 mpi writer的標準示例,它就可以實現。而otf2 mpi writer的標準示例ldd的輸出也不包含libotf2.a。您可以找到示例here

+1

請注意,以下劃線開頭的標識符後面跟着大寫字母 。 – VTT

+0

那麼,這個符號應該在哪裏定義。我沒有看到你將任何外部庫鏈接到test4 –

+0

請添加'ldd。/ libtest4.so','nm ./libtest4.so |的輸出。 grep -i OTF2_Archive_Open','nm libotf2.so | grep -i OTF2_Archive_Open'。您還可以將'-x LD_DEBUG = all'添加到mpi調用中。 – Zulan

回答

1

鏈接事項的順序。您必須在您鏈接的圖書館前擁有自己的圖書館,例如

mpic++ -shared test4.o -o libtest4.so `otf2-config --ldflags` `otf2-config --libs` 

鏈接器從左向右解析未知符號。欲瞭解更多詳情,請參閱this answer。 如果otf2.a未使用-fPIC構建,那麼這可能仍然不起作用。我建議使用--enable-shared配置otf2,並使用.so代替。