How to use thrust with PyCuda?

1.4k views Asked by At

I just want to call this code (sort algorithm provided by thrust) from python

#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/random.h>
#include <iostream>
#include <iomanip>
void initialize(thrust::device_vector<int>& v)
{
    thrust::default_random_engine rng(123456);
    thrust::uniform_int_distribution<int> dist(10, 99); // mean 10 std 99
    for(size_t i = 0; i < v.size(); i++)
    v[i] = dist(rng);
}
int main (void){
size_t N = 16;
thrust::device_vector<int> keys(N);
initialize(keys);
print(keys);
thrust::sort(keys.begin(), keys.end());
return 0;
}

So I found this example ThrustInterop.py.

And with that I got problems by setting the toolchain properly so I looked under /usr/lib/python2.7/config/Makefile

CC=     clang
CXX=        clang++
CFLAGS=     $(BASECFLAGS) -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 $(OPT) $(EXTRA_CFLAGS)
CPPFLAGS=   -I. -IInclude -I$(srcdir)/Include 
LDFLAGS=    -Wl,-F.
CONFIG_ARGS=     '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--enable-ipv6' '--with-system-expat' '--with-threads' '--enable-framework=/System/Library/Frameworks' '--enable-toolbox-glue' '--with-system-ffi' '--with-gcc=clang' 'CC=clang' 'CXX=clang++' 'CFLAGS=-g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32' 'LDFLAGS=-Wl,-F.'

I tried changing that to:

CC=     gcc
CXX=        g++

CFLAGS=     $(BASECFLAGS) -g -O2 -I/Users/Leo/local2/include -arch  x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk $(OPT) $(EXTRA_CFLAGS)
CFLAGS=     $(BASECFLAGS) -g -O2 -arch x86_64 -arch i386 -mmacosx-version-min=10.7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk $(OPT) $(EXTRA_CFLAGS)
CFLAGS=     $(BASECFLAGS) -g -O2 -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk $(OPT) $(EXTRA_CFLAGS)
CFLAGS=     $(BASECFLAGS) -g -O2 -m32 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk $(OPT) $(EXTRA_CFLAGS)
CFLAGS=     $(BASECFLAGS) -g -O2 -m64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk $(OPT) $(EXTRA_CFLAGS)

CPPFLAGS=   -I. -IInclude -I$(srcdir)/Include -arch x86_64 -arch i386 -mmacosx-version-min=10.7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk 

LDFLAGS=   -I/Users/Leo/local2/lib -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.5.sdk
LDFLAGS= -F/Library/Frameworks -framework CUDA -arch x86_64 -arch i386 -mmacosx-version-min=10.7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
LDFLAGS= -F/Library/Frameworks -framework CUDA -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk
LDFLAGS= -F/Library/Frameworks -framework CUDA -m32 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk
LDFLAGS= -F/Library/Frameworks -framework CUDA -m64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk

No matter what values I set, I moved from this:

clang++ -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -Wall -Wshorten-64-to-32 -g -Os -Wall -Wstrict-prototypes -pipe -framework CoreFoundation -bundle -undefined dynamic_lookup -Wl,-F. -u _PyMac_Error /System/Library/Frameworks/Python.framework/Versions/2.7/Python -DENABLE_DTRACE -DMACOSX -DNDEBUG -DNDEBUG -DENABLE_DTRACE -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/Users/Leo/pool2/include/ -I/usr/local/cuda//include /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/d2580b0673e96f4f0ae5c4039d0bc056/module.o /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/008d68d74904dace31665477f04b638b/gpu.o -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib -L/Users/Leo/pool2/lib -L/usr/local/cuda//lib -lcuda -lcudart -lboost_python -lpython2.7 -ldl -o /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/d2580b0673e96f4f0ae5c4039d0bc056/codepy.temp.d2580b0673e96f4f0ae5c4039d0bc056.008d68d74904dace31665477f04b638b.module.so
ld: warning: ignoring file /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/008d68d74904dace31665477f04b638b/gpu.o, file was built for i386 which is not the architecture being linked (x86_64): /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/008d68d74904dace31665477f04b638b/gpu.o
Traceback (most recent call last):
  File "ThrustInterop.py", line 87, in <module>
    module = nvcc_mod.compile(gcc_toolchain, nvcc_toolchain, debug=True)
  File "/Library/Python/2.7/site-packages/codepy/cuda.py", line 108, in compile
    mod_name, **kwargs)
  File "/Library/Python/2.7/site-packages/codepy/jit.py", line 435, in link_extension
    return load_dynamic(mod_name, destination)
ImportError: dlopen(/var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/d2580b0673e96f4f0ae5c4039d0bc056/codepy.temp.d2580b0673e96f4f0ae5c4039d0bc056.008d68d74904dace31665477f04b638b.module.so, 2): Library not loaded: libboost_python.dylib
  Referenced from: /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/d2580b0673e96f4f0ae5c4039d0bc056/codepy.temp.d2580b0673e96f4f0ae5c4039d0bc056.008d68d74904dace31665477f04b638b.module.so
  Reason: image not found

To this:

g++ -fno-strict-aliasing -g -O2 -g -fwrapv -O3 -Wall -framework CoreFoundation -bundle -undefined dynamic_lookup -u _PyMac_Error -DNDEBUG -I/Users/Leo/local2/include/python2.7 -I/Users/Leo/pool2/include/ -I/usr/local/cuda//include /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/5d0930e84f00eda6484e6423f88b81ae/module.o /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/0d69721d4d56892cb0c92be07d9ccc1d/gpu.o -L/Users/Leo/local2/lib -L/Users/Leo/pool2/lib -L/usr/local/cuda//lib -lcuda -lcudart -lboost_python -lpython2.7 -ldl -o /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/5d0930e84f00eda6484e6423f88b81ae/codepy.temp.5d0930e84f00eda6484e6423f88b81ae.0d69721d4d56892cb0c92be07d9ccc1d.module.so
ld: warning: ignoring file /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/0d69721d4d56892cb0c92be07d9ccc1d/gpu.o, file was built for i386 which is not the architecture being linked (x86_64): /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/0d69721d4d56892cb0c92be07d9ccc1d/gpu.o
Traceback (most recent call last):
  File "ThrustInterop.py", line 87, in <module>
   module = nvcc_mod.compile(gcc_toolchain, nvcc_toolchain, debug=True)
  File "/Users/Leo/local2/lib/python2.7/site-packages/codepy/cuda.py", line 95, in compile
    mod_name, **kwargs)
  File "/Users/Leo/local2/lib/python2.7/site-packages/codepy/jit.py", line 435, in link_extension
    return load_dynamic(mod_name, destination)
ImportError: dlopen(/var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/5d0930e84f00eda6484e6423f88b81ae/codepy.temp.5d0930e84f00eda6484e6423f88b81ae.0d69721d4d56892cb0c92be07d9ccc1d.module.so, 2): Library not loaded: libboost_python.dylib
  Referenced from: /var/folders/n4/l3cggyqs1jj1_ltxl8v3dljh0000gn/T/codepy-compiler-cache-v5-uid501/5d0930e84f00eda6484e6423f88b81ae/codepy.temp.5d0930e84f00eda6484e6423f88b81ae.0d69721d4d56892cb0c92be07d9ccc1d.module.so
  Reason: image not found

This is my .aksetup-defaults.py

BOOST_COMPILER = 'gcc42'
BOOST_INC_DIR = ['${HOME}/pool2/include/']
BOOST_LIB_DIR = ['${HOME}/pool2/lib']
BOOST_PYTHON_LIBNAME = ['boost_python']
BOOST_THREAD_LIBNAME = ['boost_thread']
CUDADRV_LIBNAME = ['cuda']
CUDADRV_LIB_DIR = ['${CUDA_ROOT}/lib']
CUDART_LIBNAME = ['cudart']
CUDART_LIB_DIR = ['${CUDA_ROOT}/lib']
CUDA_ENABLE_CURAND = True
CUDA_ENABLE_GL = False
CUDA_INC_DIR = ['${CUDA_ROOT}/include']
CUDA_ROOT = '/usr/local/cuda/'
CUDA_TRACE = False
CURAND_LIBNAME = ['curand']
CURAND_LIB_DIR = ['${CUDA_ROOT}/lib']
USE_SHIPPED_BOOST = True

Do you know the simplest way to call this C code from Python?

0

There are 0 answers