Révision 51
| BLAS/xGEMM/xGEMM.c (revision 51) | ||
|---|---|---|
| 1 | 1 |
/* |
| 2 |
Performs matrix multiply |
|
| 2 |
Performs matrix multiply on several BLAS implementations |
|
| 3 |
Copyleft Emmanuel QUEMENER <emmanuel.quemener@gmail.com> under GPLv3 |
|
| 3 | 4 |
|
| 5 |
2014-03-14 : Add clBLAS implementation |
|
| 6 |
|
|
| 4 | 7 |
Thanks for help from aurel32@debian.org |
| 5 | 8 |
*/ |
| 6 | 9 |
|
| ... | ... | |
| 10 | 13 |
#include <sys/time.h> |
| 11 | 14 |
#include <string.h> |
| 12 | 15 |
|
| 13 |
#ifdef CUBLAS |
|
| 16 |
#ifdef CLBLAS |
|
| 17 |
#include <clBLAS.h> |
|
| 18 |
#elif CUBLAS |
|
| 14 | 19 |
#include <cublas.h> |
| 15 | 20 |
#define CUBLAS_WRAPPER_ERROR_NOERR 0 |
| 16 | 21 |
#define CUBLAS_WRAPPER_ERROR_ALLOC 1 |
| ... | ... | |
| 27 | 32 |
#include <gsl_cblas.h> |
| 28 | 33 |
#elif ACML |
| 29 | 34 |
#include <acml.h> |
| 30 |
#include <acml_mv.h> |
|
| 31 | 35 |
#else |
| 32 | 36 |
#include <cblas.h> |
| 33 | 37 |
#endif |
| 34 | 38 |
|
| 39 |
#ifdef CLBLAS |
|
| 40 |
|
|
| 35 | 41 |
#ifdef DOUBLE |
| 42 |
#define LENGTH cl_double |
|
| 43 |
#else |
|
| 44 |
#define LENGTH cl_float |
|
| 45 |
#endif |
|
| 46 |
|
|
| 47 |
#else |
|
| 48 |
|
|
| 49 |
#ifdef DOUBLE |
|
| 36 | 50 |
#define LENGTH double |
| 37 | 51 |
#else |
| 38 | 52 |
#define LENGTH float |
| 39 | 53 |
#endif |
| 40 | 54 |
|
| 55 |
#endif |
|
| 56 |
|
|
| 41 | 57 |
#ifdef FBLAS |
| 42 | 58 |
|
| 43 | 59 |
#ifdef DOUBLE |
| ... | ... | |
| 218 | 234 |
/* Get first timer before launching */ |
| 219 | 235 |
gettimeofday(&tv1, &tz); |
| 220 | 236 |
|
| 237 |
/* Compute with CLBLAS library */ |
|
| 238 |
#ifdef CLBLAS |
|
| 239 |
|
|
| 240 |
cl_int err,errA,errB,errC,errD; |
|
| 241 |
cl_platform_id platform = 0; |
|
| 242 |
cl_device_id device = 0; |
|
| 243 |
cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
|
|
| 244 |
cl_context ctx = 0; |
|
| 245 |
cl_command_queue queue = 0; |
|
| 246 |
cl_mem bufA, bufB, bufC, bufD; |
|
| 247 |
cl_event event = NULL; |
|
| 248 |
|
|
| 249 |
struct timeval tv3,tv4; |
|
| 250 |
|
|
| 251 |
printf("Using CLBLAS: %i iterations for %ix%i matrix\n",
|
|
| 252 |
RUNS,dim,dim); |
|
| 253 |
|
|
| 254 |
/* Setup OpenCL environment. */ |
|
| 255 |
/* EQ : Select GPU on 0. Generic version to be performed */ |
|
| 256 |
err = clGetPlatformIDs( 1, &platform, NULL ); |
|
| 257 |
err = clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL ); |
|
| 258 |
|
|
| 259 |
props[1] = (cl_context_properties)platform; |
|
| 260 |
ctx = clCreateContext( props, 1, &device, NULL, NULL, &err ); |
|
| 261 |
queue = clCreateCommandQueue( ctx, device, 0, &err ); |
|
| 262 |
|
|
| 263 |
/* Setup clBLAS */ |
|
| 264 |
err = clblasSetup( ); |
|
| 265 |
|
|
| 266 |
/* Prepare OpenCL memory objects and place matrices inside them. */ |
|
| 267 |
bufA = clCreateBuffer( ctx, CL_MEM_READ_ONLY, dim*dim * sizeof(*A), |
|
| 268 |
NULL, &errA ); |
|
| 269 |
bufB = clCreateBuffer( ctx, CL_MEM_READ_ONLY, dim*dim * sizeof(*B), |
|
| 270 |
NULL, &errB ); |
|
| 271 |
bufC = clCreateBuffer( ctx, CL_MEM_READ_WRITE, dim*dim * sizeof(*C), |
|
| 272 |
NULL, &errC ); |
|
| 273 |
bufD = clCreateBuffer( ctx, CL_MEM_READ_WRITE, dim*dim * sizeof(*D), |
|
| 274 |
NULL, &errD ); |
|
| 275 |
|
|
| 276 |
errA = clEnqueueWriteBuffer( queue, bufA, CL_TRUE, 0, |
|
| 277 |
dim * dim * sizeof( *A ), A, 0, NULL, NULL ); |
|
| 278 |
errB = clEnqueueWriteBuffer( queue, bufB, CL_TRUE, 0, |
|
| 279 |
dim * dim * sizeof( *B ), B, 0, NULL, NULL ); |
|
| 280 |
errC = clEnqueueWriteBuffer( queue, bufC, CL_TRUE, 0, |
|
| 281 |
dim * dim * sizeof( *C ), C, 0, NULL, NULL ); |
|
| 282 |
errD = clEnqueueWriteBuffer( queue, bufD, CL_TRUE, 0, |
|
| 283 |
dim * dim * sizeof( *D ), D, 0, NULL, NULL ); |
|
| 284 |
|
|
| 285 |
/* Get third timer after memory operation */ |
|
| 286 |
gettimeofday(&tv3, &tz); |
|
| 287 |
|
|
| 288 |
#ifdef DOUBLE |
|
| 289 |
|
|
| 290 |
for (i=0;i<RUNS;i++) |
|
| 291 |
{
|
|
| 292 |
err = clblasDgemm( clblasRowMajor, clblasNoTrans, clblasNoTrans, |
|
| 293 |
dim, dim, dim, |
|
| 294 |
alpha, bufA, 0, dim, |
|
| 295 |
bufB, 0, dim, beta, |
|
| 296 |
bufC, 0, dim, |
|
| 297 |
1, &queue, 0, NULL, &event ); |
|
| 298 |
|
|
| 299 |
err = clblasDgemm( clblasRowMajor, clblasTrans, clblasTrans, |
|
| 300 |
dim, dim, dim, |
|
| 301 |
alpha, bufB, 0, dim, |
|
| 302 |
bufA, 0, dim, beta, |
|
| 303 |
bufD, 0, dim, |
|
| 304 |
1, &queue, 0, NULL, &event ); |
|
| 305 |
|
|
| 306 |
} |
|
| 307 |
|
|
| 308 |
#else |
|
| 309 |
|
|
| 310 |
for (i=0;i<RUNS;i++) |
|
| 311 |
{
|
|
| 312 |
|
|
| 313 |
err = clblasSgemm( clblasRowMajor, clblasNoTrans, clblasNoTrans, |
|
| 314 |
dim, dim, dim, |
|
| 315 |
alpha, bufA, 0, dim, |
|
| 316 |
bufB, 0, dim, beta, |
|
| 317 |
bufC, 0, dim, |
|
| 318 |
1, &queue, 0, NULL, &event ); |
|
| 319 |
|
|
| 320 |
err = clblasSgemm( clblasRowMajor, clblasTrans, clblasTrans, |
|
| 321 |
dim, dim, dim, |
|
| 322 |
alpha, bufB, 0, dim, |
|
| 323 |
bufA, 0, dim, beta, |
|
| 324 |
bufD, 0, dim, |
|
| 325 |
1, &queue, 0, NULL, &event ); |
|
| 326 |
} |
|
| 327 |
|
|
| 328 |
#endif |
|
| 329 |
|
|
| 330 |
/* Wait for calculations to be finished. */ |
|
| 331 |
err = clWaitForEvents( 1, &event ); |
|
| 332 |
|
|
| 333 |
/* Fetch results of calculations from GPU memory. */ |
|
| 334 |
err = clEnqueueReadBuffer( queue, bufC, CL_TRUE, 0, |
|
| 335 |
dim * dim * sizeof(*C), |
|
| 336 |
C, 0, NULL, NULL ); |
|
| 337 |
|
|
| 338 |
/* Fetch results of calculations from GPU memory. */ |
|
| 339 |
err = clEnqueueReadBuffer( queue, bufD, CL_TRUE, 0, |
|
| 340 |
dim * dim * sizeof(*D), |
|
| 341 |
D, 0, NULL, NULL ); |
|
| 342 |
|
|
| 343 |
/* Release OpenCL memory objects. */ |
|
| 344 |
clReleaseMemObject( bufD ); |
|
| 345 |
clReleaseMemObject( bufC ); |
|
| 346 |
clReleaseMemObject( bufB ); |
|
| 347 |
clReleaseMemObject( bufA ); |
|
| 348 |
|
|
| 349 |
/* Finalize work with clBLAS */ |
|
| 350 |
clblasTeardown( ); |
|
| 351 |
|
|
| 352 |
/* Release OpenCL working objects. */ |
|
| 353 |
clReleaseCommandQueue( queue ); |
|
| 354 |
clReleaseContext( ctx ); |
|
| 355 |
|
|
| 356 |
/* if ((stat1 != CUBLAS_STATUS_SUCCESS) ) { */
|
|
| 357 |
/* wrapperError ("xGEMM", CUBLAS_WRAPPER_ERROR_GET); */
|
|
| 358 |
/* } */ |
|
| 359 |
|
|
| 360 |
/* Get fourth timer after memory free */ |
|
| 361 |
gettimeofday(&tv4, &tz); |
|
| 362 |
|
|
| 221 | 363 |
/* Compute with CuBLAS library */ |
| 222 |
#ifdef CUBLAS
|
|
| 364 |
#elif CUBLAS
|
|
| 223 | 365 |
LENGTH *devPtrA=0, *devPtrB=0, *devPtrC=0, *devPtrD=0; |
| 224 | 366 |
cublasStatus stat1, stat2, stat3, stat4; |
| 225 | 367 |
struct timeval tv3,tv4; |
| BLAS/xGEMM/bench.sh (revision 51) | ||
|---|---|---|
| 64 | 64 |
|
| 65 | 65 |
CUBLAS=$(./${BENCH}_${FORMAT}_cublas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | tr "\n" " " | awk '{ print $5"\t"$1"\t"$3 }')
|
| 66 | 66 |
|
| 67 |
CBLAS=$(./${BENCH}_${FORMAT}_cblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 67 |
# CBLAS=$(./${BENCH}_${FORMAT}_cblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 68 | 68 |
|
| 69 |
FBLAS=$(./${BENCH}_${FORMAT}_fblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 69 |
# FBLAS=$(./${BENCH}_${FORMAT}_fblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 70 | 70 |
|
| 71 | 71 |
# GSL=$(./${BENCH}_${FORMAT}_gsl $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
| 72 | 72 |
|
| 73 |
OPENBLAS=$(./${BENCH}_${FORMAT}_openblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 73 |
# OPENBLAS=$(./${BENCH}_${FORMAT}_openblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 74 | 74 |
|
| 75 | 75 |
# ACML=$(./${BENCH}_${FORMAT}_acml $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
| 76 | 76 |
|
| ... | ... | |
| 97 | 97 |
|
| 98 | 98 |
CUBLAS=$(./${BENCH}_${FORMAT}_cublas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | tr "\n" " " | awk '{ print $5"\t"$1"\t"$3 }')
|
| 99 | 99 |
|
| 100 |
CBLAS=$(./${BENCH}_${FORMAT}_cblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 100 |
# CBLAS=$(./${BENCH}_${FORMAT}_cblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 101 | 101 |
|
| 102 |
FBLAS=$(./${BENCH}_${FORMAT}_fblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 102 |
# FBLAS=$(./${BENCH}_${FORMAT}_fblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 103 | 103 |
|
| 104 | 104 |
# GSL=$(./${BENCH}_${FORMAT}_gsl $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
| 105 | 105 |
|
| 106 |
OPENBLAS=$(./${BENCH}_${FORMAT}_openblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 106 |
# OPENBLAS=$(./${BENCH}_${FORMAT}_openblas $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
|
| 107 | 107 |
|
| 108 | 108 |
# ACML=$(./${BENCH}_${FORMAT}_acml $SIZE $NUMBER | grep GFlops | awk -F: '{ print $2 }' | awk '{ print $1 }')
|
| 109 | 109 |
|
| BLAS/xGEMM/Makefile (revision 51) | ||
|---|---|---|
| 9 | 9 |
CUDASRCINC=fortran_common.h |
| 10 | 10 |
CUDAINC=/usr/include |
| 11 | 11 |
CUDALIB=/usr/lib/x86_64-linux-gnu/ |
| 12 |
CLBLASINC=/opt/clBLAS/build/include |
|
| 13 |
CLBLASLIB=/opt/clBLAS/build/library/ |
|
| 12 | 14 |
|
| 13 | 15 |
PATCHTHUNKING=patch_thunking.h |
| 14 | 16 |
|
| ... | ... | |
| 24 | 26 |
|
| 25 | 27 |
ATLAS=/usr/lib/atlas-base |
| 26 | 28 |
|
| 27 |
EXECUTABLE=cblas fblas gsl cublas thunking openblas |
|
| 29 |
EXECUTABLE=cblas fblas gsl cublas thunking openblas acml clblas
|
|
| 28 | 30 |
|
| 29 | 31 |
FORMAT=DOUBLE |
| 30 | 32 |
#FORMAT=FLOAT |
| ... | ... | |
| 53 | 55 |
|
| 54 | 56 |
openblas: $(SOURCE) |
| 55 | 57 |
|
| 56 |
$(CC) $(CFLAGS) $(DIRECTIVES) -DFLOAT -DCBLAS $(LDFLAGS) \ |
|
| 58 |
$(CC) $(CFLAGS) $(DIRECTIVES) -fopenmp -DFLOAT -DCBLAS $(LDFLAGS) \
|
|
| 57 | 59 |
$(SOURCE) $(OPENBLAS)/libopenblas.a -lpthread -o $(SOURCE:.c=)_SP_$@ |
| 58 | 60 |
|
| 59 |
$(CC) $(CFLAGS) $(DIRECTIVES) -DDOUBLE -DCBLAS $(LDFLAGS) \ |
|
| 61 |
$(CC) $(CFLAGS) $(DIRECTIVES) -fopenmp -DDOUBLE -DCBLAS $(LDFLAGS) \
|
|
| 60 | 62 |
$(SOURCE) $(OPENBLAS)/libopenblas.a -lpthread -o $(SOURCE:.c=)_DP_$@ |
| 61 | 63 |
|
| 62 | 64 |
acml: $(SOURCE) |
| 63 | 65 |
|
| 64 | 66 |
$(CC) -I$(ACMLINC) $(CFLAGS) $(DIRECTIVES) -DFLOAT -DACML $(LDFLAGS) \ |
| 65 |
$(SOURCE) -L$(ACMLLIB) -lacml_mp -lacml_mv \
|
|
| 67 |
$(SOURCE) -L$(ACMLLIB) -lacml_mp \ |
|
| 66 | 68 |
-lgomp -lgfortran -lpthread -o $(SOURCE:.c=)_SP_$@ |
| 67 | 69 |
|
| 68 | 70 |
$(CC) -I$(ACMLINC) $(CFLAGS) $(DIRECTIVES) -DDOUBLE -DACML $(LDFLAGS) \ |
| 69 |
$(SOURCE) -L$(ACMLLIB) -lacml_mp -lacml_mv \
|
|
| 71 |
$(SOURCE) -L$(ACMLLIB) -lacml_mp \ |
|
| 70 | 72 |
-lgomp -lgfortran -lpthread -o $(SOURCE:.c=)_DP_$@ |
| 71 | 73 |
|
| 72 | 74 |
fblas: $(SOURCE) |
| ... | ... | |
| 88 | 90 |
-DGSL $(LDFLAGS) \ |
| 89 | 91 |
$(SOURCE) -lgslcblas -o $(SOURCE:.c=)_DP_$@ |
| 90 | 92 |
|
| 93 |
clblas: $(SOURCE) |
|
| 94 |
|
|
| 95 |
$(CC) -I$(CLBLASINC) -L$(CLBLASLIB) $(CFLAGS) -DFLOAT \ |
|
| 96 |
-DCLBLAS $(LDFLAGS) \ |
|
| 97 |
$(DIRECTIVES) $(SOURCE) -lclBLAS -lOpenCL -o $(SOURCE:.c=)_SP_$@ |
|
| 98 |
|
|
| 99 |
$(CC) -I$(CLBLASINC) -L$(CLBLASLIB) $(CFLAGS) -DDOUBLE \ |
|
| 100 |
-DCLBLAS $(LDFLAGS) \ |
|
| 101 |
$(DIRECTIVES) $(SOURCE) -lclBLAS -lOpenCL -o $(SOURCE:.c=)_DP_$@ |
|
| 102 |
|
|
| 91 | 103 |
cublas: $(SOURCE) |
| 92 | 104 |
|
| 93 | 105 |
$(CC) -I$(CUDAINC) -L$(CUDALIB) $(CFLAGS) -DFLOAT \ |
Formats disponibles : Unified diff