2015-08-14 169 views
0

我正尝试使用CHOLMODSuiteSparse 4.4.4中的CUDA加速。我根据用户指南进行编译,我可以在Demo文件夹下成功运行gpu.sh,这表明GPU正在完成部分工作。但是,当我尝试使用CHOLMOD来运行我自己的代码时,我发现GPU调用的数量始终为0.我将Common->useGPU设置为1,环境变量CHOLMOD_USE_GPU也设置为1.我的Makefile如下所示。库路径是正确的。对我有何建议?不能在我自己的代码中使用带有CUDA加速的CHOLMOD

其实我应该提到我只是运行一个最简单的测试用例来解决线性系统问题。

我试过了UF Sparse Matrix Collection的几个矩阵,但nvprof显示没有CUDA应用程序被剖析。

一些我试过矩阵:

bmw7st_1:http://www.cise.ufl.edu/research/sparse/matrices/GHS_psdef/bmw7st_1.html

nd6k: http://www.cise.ufl.edu/research/sparse/matrices/ND/nd6k.html

nd24k: http://www.cise.ufl.edu/research/sparse/matrices/ND/nd24k.html

代码:

#include <stdio.h> 
#include <time.h> 
#include <unistd.h> 
#include <assert.h> 
#include <sys/time.h> 
#include "cholmod.h" 

int main (void) 
{ 
    struct timeval t1, t2; 
    double elapsedTime; 

    const char* matFile = "../bmw7st_1.mtx"; 
    FILE* fp = fopen(matFile, "r"); 
    assert(fp != NULL); 

    cholmod_sparse *A ; 
    cholmod_dense *x, *b; 
    cholmod_factor *L ; 

    cholmod_common* c = (cholmod_common*)malloc(sizeof(cholmod_common)); 
    cholmod_start (c) ; /* start CHOLMOD */ 
    c->useGPU = 1; 
    c->supernodal = CHOLMOD_SUPERNODAL; 

    A = cholmod_read_sparse (fp, c) ; /* read in a matrix */ 
    cholmod_print_sparse (A, "A", c) ; /* print the matrix */ 
    fclose(fp); 

    if (A == NULL || A->stype == 0) /* A must be symmetric */ 
    { 
     cholmod_free_sparse (&A, c) ; 
     cholmod_finish (c) ; 
     return (0) ; 
    } 

    b = cholmod_ones (A->nrow, 1, A->xtype, c) ; /* b = ones(n,1) */ 

    gettimeofday(&t1, NULL); 
    L = cholmod_analyze (A, c) ; /* analyze */ 
    cholmod_factorize (A, L, c) ; /* factorize */ 
    x = cholmod_solve (CHOLMOD_A, L, b, c) ; /* solve Ax=b */ 
    gettimeofday(&t2, NULL); 
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; 
    elapsedTime += (t2.tv_usec - t1.tv_usec)/1000.0; 
    printf("Time: %.4f ms\n", elapsedTime); 

    cholmod_free_factor (&L, c) ; /* free matrices */ 
    cholmod_free_sparse (&A, c) ; 
    cholmod_free_dense (&x, c) ; 
    cholmod_free_dense (&b, c) ; 
    cholmod_finish (c) ; /* finish CHOLMOD */ 
    return (0) ; 
} 

生成文件:

CC = gcc 

CFLAGS = -g -Wall -O2 \ 
-lrt -lgfortran \ 
-gdwarf-2 

LIBS = $(CHOLMOD)/Lib/libcholmod.a \ 
$(AMD)/Lib/libamd.a \ 
$(COLAMD)/Lib/libcolamd.a \ 
$(LAPACK)/liblapack.a \ 
$(OPENBLAS)/lib/libopenblas.so \ 
$(XERBLA)/libcerbla.a \ 
$(METIS)/libmetis.a \ 
$(CAMD)/Lib/libcamd.a \ 
$(CCOLAMD)/Lib/libccolamd.a \ 
$(SUITESPARSE)/SuiteSparse_config/libsuitesparseconfig.a \ 
$(CUDART_LIB) \ 
$(CUBLAS_LIB) 

HEADER_DIR = $(CHOLMOD)/Include 
CONFIG_HEADER_DIR = $(SUITESPARSE)/SuiteSparse_config 

OBJ_DIR = . 

BIN_DIR = . 

INCLUDES = -I$(HEADER_DIR) \ 
-I$(CONFIG_HEADER_DIR) 

SRCS = $(shell ls *.c) 

OBJS = $(SRCS:.c=.o) 

OBJS_BUILD = $(shell ls $(OBJ_DIR)/*.o) 

APP = prog 

RM = rm -f 

all: $(APP) 

$(APP): $(OBJS) 
     $(CC) $(CFLAGS) -o $(BIN_DIR)/$(APP) $(OBJS_BUILD) $(LIBS) 

%.o: %.c $(HEADER_DIR)/*.h $(CONFIG_HEADER_DIR)/*.h 
     $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $(OBJ_DIR)/[email protected] 

clean: 
     $(RM) $(OBJS_BUILD) $(APP) 
+1

实际上你应该提供一个完整的测试用例,而不仅仅是一个makefile。 –

+0

@RobertCrovella对不起,添加了测试用例。 –

+2

好吧,我想没有矩阵文件的话,没人会尝试你的测试用例。你可以使用某人可以抓取的标准矩阵文件吗?矩阵有多大?我很确定CHOLMOD(即SuiteSparse)能够对是否使用GPU做出明智的决定。小型测试用例可能不会使用GPU。 –

回答

1

参照部7,CHOLMOD UserGuide.pdf的P34附带SuiteSparse 4.4.4:

只有CHOLMOD的长整数版本可以利用GPU加速。

长整数版本通过API调用来区分,如cholmod_l_start而不是cholmod_start

进行了以下修改到你的程序:

#include <stdio.h> 
#include <time.h> 
#include <unistd.h> 
#include <assert.h> 
#include <sys/time.h> 
#include "cholmod.h" 

int main (void) 
{ 
    struct timeval t1, t2; 
    double elapsedTime; 

    const char* matFile = "../Matrix/nd6k/nd6k.mtx"; 
    FILE* fp = fopen(matFile, "r"); 
    assert(fp != NULL); 

    cholmod_sparse *A ; 
    cholmod_dense *x, *b; 
    cholmod_factor *L ; 

    cholmod_common* c = (cholmod_common*)malloc(sizeof(cholmod_common)); 
    cholmod_l_start (c) ; /* start CHOLMOD */ 
    c->useGPU = 1; 
    c->supernodal = CHOLMOD_SUPERNODAL; 

    A = cholmod_l_read_sparse (fp, c) ; /* read in a matrix */ 
    cholmod_l_print_sparse (A, "A", c) ; /* print the matrix */ 
    fclose(fp); 

    if (A == NULL || A->stype == 0) /* A must be symmetric */ 
    { 
     cholmod_l_free_sparse (&A, c) ; 
     cholmod_l_finish (c) ; 
     return (0) ; 
    } 

    b = cholmod_l_ones (A->nrow, 1, A->xtype, c) ; /* b = ones(n,1) */ 

    gettimeofday(&t1, NULL); 
    L = cholmod_l_analyze (A, c) ; /* analyze */ 
    cholmod_l_factorize (A, L, c) ; /* factorize */ 
    x = cholmod_l_solve (CHOLMOD_A, L, b, c) ; /* solve Ax=b */ 
    gettimeofday(&t2, NULL); 
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; 
    elapsedTime += (t2.tv_usec - t1.tv_usec)/1000.0; 
    printf("Time: %.4f ms\n", elapsedTime); 
    cholmod_l_gpu_stats(c); 
    cholmod_l_free_factor (&L, c) ; /* free matrices */ 
    cholmod_l_free_sparse (&A, c) ; 
    cholmod_l_free_dense (&x, c) ; 
    cholmod_l_free_dense (&b, c) ; 
    cholmod_l_finish (c) ; /* finish CHOLMOD */ 
    return (0) ; 
} 

我得到的输出是这样的:

$ ./prog 
CHOLMOD sparse: A: 18000-by-18000, nz 3457658, upper. OK 
Time: 14570.3950 ms 

CHOLMOD GPU/CPU statistics: 
SYRK CPU calls   888 time 1.0637e-01 
     GPU calls   213 time 8.9194e-02 
GEMM CPU calls   711 time 1.1511e-01 
     GPU calls   213 time 1.9351e-03 
POTRF CPU calls   217 time 3.2180e-02 
     GPU calls   5 time 1.5788e-01 
TRSM CPU calls   217 time 6.0409e-01 
     GPU calls   4 time 5.6943e-02 
time in the BLAS: CPU 8.5774e-01 GPU 3.0595e-01 total: 1.1637e+00 
assembly time 0.0000e+00 0.0000e+00 
$ 

表明正在使用的GPU。

+0

非常感谢!我是SuiteSparse的新手,我错过了那么重要的信息。它终于有效。 –