带有 CUDA 函数的 PHP 扩展

PHP extension with CUDA function

本文关键字:PHP 扩展 函数 CUDA 带有      更新时间:2023-10-16

如何使用 CUDA 构建 PHP 扩展?

如何使用 CUDA 构建 PHP 扩展?我无法生成此示例?我不明白。你可以帮我吗?谢谢。


系统:

Ubuntu 12.04 x64 (3.2.0-54-generic #82-Ubuntu SMP Tue Sep 10 20:08:42 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux)
GCC 4.6.3
CUDA 5.5  (http://developer.download.nvidia.com/compute/cuda/5_5/rel/installers/cuda_5.5.22_linux_64.run)
Cuda compilation tools release 5.5, V5.5.0
PHP 5.3.10-1
Zend Engine 2.3.0
libtool (GNU libtool) 2.4.2

简介:

install:
sudo apt-get install php5-dev
build:
phpize
./configure --enable-cuda_ext CC=nvcc
make
verify:
php-config | grep extension-dir
php -d extension=cuda_ext.so -m
config:
sudo vim /etc/php5/apache2/php.ini:
extension=cuda_ext.so
files:
config.m4
php_cuda_ext.h
cuda_ext.cu

配置

$ phpize --clean && phpize && ./configure --enable-cuda_ext CC=nvcc
Cleaning..
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether nvcc accepts -g... yes
checking for nvcc option to accept ISO C89... none needed
checking how to run the C preprocessor... nvcc -E
checking for icc... no
checking for suncc... no
checking whether nvcc and cc understand -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... no
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib
checking for PHP extension directory... /usr/lib/php5/20090626
checking for PHP installed headers prefix... /usr/include/php5
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... nawk
checking if nawk is broken... no
checking whether to enable cuda_ext support... yes, shared
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking how to print strings... printf
checking for a sed that does not truncate output... (cached) /bin/sed
checking for fgrep... /bin/grep -F
checking for ld used by nvcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %sn
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... (cached) nawk
checking command to parse /usr/bin/nm -B output from nvcc object... ok
checking for sysroot... no
checking for mt... mt
checking if mt is a manifest tool... no
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if nvcc supports -fno-rtti -fno-exceptions... no
checking for nvcc option to produce PIC... -Xcompiler -fPIC -DPIC
checking if nvcc PIC flag -Xcompiler -fPIC -DPIC works... yes
checking if nvcc static flag -static works... no
checking if nvcc supports -c -o file.o... yes
checking if nvcc supports -c -o file.o... (cached) yes
checking whether the nvcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... yes
checking dynamic linker characteristics... nvcc fatal   : Unknown option 'print-search-dirs'
GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /usr/bin/ld -m elf_x86_64
checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands

***** config.m4 *****
dnl config.m4 for cuda_ext
PHP_ARG_ENABLE(cuda_ext,
    [whether to enable cuda_ext support],
    [  --enable-cuda_ext          Enable cuda_ext support])
if test "$PHP_CUDA_EXT" != "no"; then
    AC_DEFINE(HAVE_CUDA_EXT, 1, [Whether you have cuda_ext support])
    PHP_REQUIRE_CXX()
    PHP_ADD_INCLUDE(/usr/local/cuda-5.5/include)
    PHP_ADD_INCLUDE(/opt/libs/thrust)
    PHP_ADD_LIBRARY(stdc++, 1, TEST_SHARED_LIBADD)
    PHP_NEW_EXTENSION(cuda_ext, cuda_main.cu, $ext_shared, , "", "yes")
fi

***** php_cuda_ext.h *****
PHP_FUNCTION(cuda_function);

***** cuda_main.cu *****
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
extern "C" {
    #include "php.h"
    #include "php_ini.h"
    #include "ext/standard/info.h"
    #include "Zend/zend.h"
    #include "Zend/zend_hash.h"
}
#include "php_cuda_ext.h"

ZEND_BEGIN_ARG_INFO(arginfo_cuda_function, 0)
    ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO();
zend_function_entry cuda_ext_functions[] =
{
    ZEND_FE(cuda_function, arginfo_cuda_function)
    {NULL, NULL, NULL} /* Marks the end of function entries */
};
// cuda_ext_module_entry
zend_module_entry cuda_ext_module_entry = {
    STANDARD_MODULE_HEADER,
    "cuda_ext",
    cuda_ext_functions, /* Function entries */
    NULL, /* Module init */
    NULL, /* Module shutdown */
    NULL, /* Request init */
    NULL, /* Request shutdown */
    NULL, /* Module information */
    "0.1", /* Replace with version number for your extension */
    STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_CUDA_EXT
    extern "C" {
    ZEND_GET_MODULE(cuda_ext)
    }
#endif
PHP_FUNCTION(cuda_function)
{
    int   irv   = 0;
    zval *value = NULL;
    irv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value);
    if (irv == FAILURE) {
        RETURN_FALSE;
    }
    switch (Z_TYPE_P(value)) {
        case IS_NULL:
            php_printf("type IS_NULL ");
            break;
        case IS_LONG:
            php_printf("type IS_LONG ");
            php_printf("Computing %ld * 2: %ld", Z_LVAL_P(value), Z_LVAL_P(value)*2);
            break;
        default:
            break;
    }
}
#include "algorithm"
#include "cstdlib"
#include "thrust/host_vector.h"
#include "thrust/device_vector.h"
#include "thrust/generate.h"
#include "thrust/sort.h"
#include "thrust/copy.h"
#include "thrust/reduce.h"
int cuda_test()
{
    // generate 32M random numbers serially
    thrust::host_vector<int> h_vec(32 << 20);
    std::generate(h_vec.begin(), h_vec.end(), rand);
    // transfer data to the device
    thrust::device_vector<int> d_vec = h_vec;
    // sort data on the device (846M keys per second on GeForce GTX 480)
    thrust::sort(d_vec.begin(), d_vec.end());
    // transfer data back to host
    thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
    return 0;
}

当我编译时:

/bin/bash /home/skynowa/Projects/CCuda/PHP_module/libtool --mode=link nvcc -DPHP_ATOM_INC -I/home/skynowa/Projects/CCuda/PHP_module/include -I/home/skynowa/Projects/CCuda/PHP_module/main -I/home/skynowa/Projects/CCuda/PHP_module -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -I/usr/local/cuda-5.5/include -I/opt/libs/thrust  -DHAVE_CONFIG_H  -g -O2   -o cuda_ext.la -export-dynamic -avoid-version -prefer-pic -module -rpath /home/skynowa/Projects/CCuda/PHP_module/modules  cuda_main.lo 
libtool: link: `cuda_main.lo' is not a valid libtool object
make: *** [cuda_ext.la] Error 1

怎么了??谢谢。

我想,你忘了执行命令:

 $ sudo make install

请在执行此命令后检查,这会将文件移动到默认的动态扩展目录路径。在你的PHP中提到这条路径。.INI。

我希望这有所帮助。