Révision 53

BLAS/xGEMM/Makefile (revision 53)
1 1
SOURCE=xGEMM.c
2 2

  
3
CC=gcc-4.6
4
FC=gfortran-4.6
5
CFLAGS=-Wall -O3
3
CC=gcc-4.7
4
FC=gfortran-4.7
5
CFLAGS=-Wall -O3 -ffast-math
6 6
LDFLAGS=-lm
7 7
CUDASRC=/usr/share/doc/nvidia-cuda-doc/examples/
8 8
THUNKING=fortran_thunking.c
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
# CLBLASINC=/opt/clBLAS/build/include
13
# CLBLASLIB=/opt/clBLAS/build/library/
14
CLBLASINC=/opt/clBLAS/src
15
CLBLASLIB=/opt/clBLAS/src/library/
14 16

  
15 17
PATCHTHUNKING=patch_thunking.h
16 18

  
BLAS/xGEMM/xGEMM.c (revision 53)
15 15

  
16 16
#ifdef CLBLAS
17 17
#include <clBLAS.h>
18
/* Precise here to avoid new specific bench function */
19
int MyPlatform;
20
int MyDevice;
18 21
#elif CUBLAS
19 22
#include <cublas.h>
20 23
#define CUBLAS_WRAPPER_ERROR_NOERR      0
......
237 240
  /* Compute with CLBLAS library  */
238 241
#ifdef CLBLAS
239 242

  
243
  cl_uint platformCount;
244
  cl_platform_id* platforms;
245
  cl_uint deviceCount;
246
  cl_device_id* devices;
247

  
240 248
  cl_int err,errA,errB,errC,errD;
241
  cl_platform_id platform = 0;
242
  cl_device_id device = 0;
243 249
  cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
244 250
  cl_context ctx = 0;
245 251
  cl_command_queue queue = 0;
246 252
  cl_mem bufA, bufB, bufC, bufD;
247 253
  cl_event event = NULL;
248 254

  
255
  char* value;
256
  size_t valueSize;
257

  
258
  // tv3 Put on Device: Allocate & Write buffer
259
  // tv4 Compute
249 260
  struct timeval tv3,tv4;
250 261

  
251
  printf("Using CLBLAS: %i iterations for %ix%i matrix\n",
252
	 RUNS,dim,dim);
262
  printf("Using CLBLAS: %i iterations for %ix%i matrix on (%d,%d)\n",
263
	 RUNS,dim,dim,MyPlatform,MyDevice);
253 264

  
254 265
  /* 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 );
266
  /* - get all platforms and select MyPlatform */
267
  /* - get all devices from MyPlatform and select MyDevice */
262 268

  
269
  // Get all platforms
270
  err = clGetPlatformIDs(0, NULL, &platformCount);
271
  platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
272
  err = clGetPlatformIDs(platformCount, platforms, NULL);
273

  
274
  // Get Device defined
275
  err = clGetDeviceIDs(platforms[MyPlatform], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount);
276
  devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
277
  err = clGetDeviceIDs(platforms[MyPlatform], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL);  
278

  
279
  // print device name
280
  err = clGetDeviceInfo(devices[MyDevice], CL_DEVICE_NAME, 0, NULL, &valueSize);
281
  value = (char*) malloc(valueSize);
282
  err = clGetDeviceInfo(devices[MyDevice], CL_DEVICE_NAME, valueSize, value, NULL);
283
  printf("Device (%d,%d): %s\n",MyPlatform,MyDevice, value);
284
  free(value);
285

  
286
  props[1] = (cl_context_properties)platforms[MyPlatform];
287

  
288
  /* Initialize Context */
289
  ctx = clCreateContext( props, 1, &devices[MyDevice], NULL, NULL, &err );
290
  queue = clCreateCommandQueue( ctx, devices[MyDevice], 0, &err );
291

  
263 292
  /* Setup clBLAS */
264 293
  err = clblasSetup( );
265 294

  
......
295 324

  
296 325
    }
297 326
  
327
  if (err != CL_SUCCESS) {
328
    printf("clblasDgemm() failed with %d\n", err);
329
  }
330

  
298 331
#else
299 332

  
300 333
  for (i=0;i<RUNS;i++)
......
309 342
			 bufD,0,dim,1,&queue,0,NULL,&event );
310 343
    }
311 344
  
345
  if (err != CL_SUCCESS) {
346
    printf("clblasSgemm() failed with %d\n", err);
347
  }
348

  
312 349
#endif
313 350

  
314 351
  /* Wait for calculations to be finished. */
315 352
  err = clWaitForEvents( 1, &event );
316 353
  
354
  /* Get fourth timer after memory free */
355
  gettimeofday(&tv4, &tz);
356

  
317 357
  /* Fetch results of calculations from GPU memory. */
318
  err = clEnqueueReadBuffer( queue,bufC,CL_TRUE,0,dim*dim * sizeof(*C),
358
  errC = clEnqueueReadBuffer( queue,bufC,CL_TRUE,0,dim*dim * sizeof(*C),
319 359
			     C,0,NULL,NULL );
320 360

  
321 361
  /* Fetch results of calculations from GPU memory. */
322
  err = clEnqueueReadBuffer( queue,bufD,CL_TRUE,0,dim*dim*sizeof(*D),
362
  errD = clEnqueueReadBuffer( queue,bufD,CL_TRUE,0,dim*dim*sizeof(*D),
323 363
			     D,0,NULL,NULL );
324 364

  
325 365
  /* Release OpenCL memory objects. */
......
335 375
  clReleaseCommandQueue( queue );
336 376
  clReleaseContext( ctx );
337 377
  
338
  /* Get fourth timer after memory free */
339
  gettimeofday(&tv4, &tz);
340 378

  
341 379
  /* Compute with CuBLAS library  */
342 380
#elif CUBLAS
......
413 451
  stat3=cublasGetMatrix(dim,dim,sizeof(C[0]),devPtrC,dim,C,dim);
414 452
  stat4=cublasGetMatrix(dim,dim,sizeof(D[0]),devPtrD,dim,D,dim);
415 453
  
454
  /* Get fourth timer before memory free */
455
  gettimeofday(&tv4, &tz);
456

  
416 457
  cublasFree (devPtrA);
417 458
  cublasFree (devPtrB);
418 459
  cublasFree (devPtrC);
......
422 463
    wrapperError ("xGEMM", CUBLAS_WRAPPER_ERROR_GET);
423 464
  }
424 465
  
425
  /* Get fourth timer after memory free */
426
  gettimeofday(&tv4, &tz);
427 466

  
428 467
#elif THUNKING
429 468
  
......
436 475

  
437 476
  for (i=0;i<RUNS;i++)
438 477
    {      
439
      /* Multiply A by X as Y <- A.X */
440 478
      CUBLAS_DGEMM(&transa,&transa,
441 479
      		   &dim,&dim,&dim,&alpha,B,&dim,A,&dim,&beta,C,&dim);
442 480
      CUBLAS_DGEMM(&transb,&transb,
......
447 485

  
448 486
  for (i=0;i<RUNS;i++)
449 487
    {      
450
      /* Multiply A by X as Y <- A.X */
451 488
      CUBLAS_SGEMM(&transa,&transa,
452 489
      		   &dim,&dim,&dim,&alpha,B,&dim,A,&dim,&beta,C,&dim);
453 490
      CUBLAS_SGEMM(&transb,&transb,
......
468 505

  
469 506
  for (i=0;i<RUNS;i++)
470 507
    {    
471
      /* Multiply A by X as Y <- A.X */
472 508
      dgemm_(&transa,&transa,&dim,&dim,&dim,&alpha,B,&dim,A,&dim,&beta,C,&dim);
473 509
      dgemm_(&transb,&transb,&dim,&dim,&dim,&alpha,A,&dim,B,&dim,&beta,D,&dim);
474 510
    }
......
477 513

  
478 514
  for (i=0;i<RUNS;i++)
479 515
    {    
480
      /* Multiply A by X as Y <- A.X */
481 516
      sgemm_(&transa,&transa,&dim,&dim,&dim,&alpha,B,&dim,A,&dim,&beta,C,&dim);
482 517
      sgemm_(&transb,&transb,&dim,&dim,&dim,&alpha,A,&dim,B,&dim,&beta,D,&dim);
483 518
    }
......
496 531

  
497 532
  for (i=0;i<RUNS;i++)
498 533
    {    
499
      /* Multiply A by X as Y <- A.X */
500 534
      dgemm(transa,transa,dim,dim,dim,alpha,B,dim,A,dim,beta,C,dim);
501 535
      dgemm(transb,transb,dim,dim,dim,alpha,A,dim,B,dim,beta,D,dim);
502 536
    }
......
505 539

  
506 540
  for (i=0;i<RUNS;i++)
507 541
    {    
508
      /* Multiply A by X as Y <- A.X */
509 542
      sgemm(transa,transa,dim,dim,dim,alpha,B,dim,A,dim,beta,C,dim);
510 543
      sgemm(transb,transb,dim,dim,dim,alpha,A,dim,B,dim,beta,D,dim);
511 544
    }
......
527 560

  
528 561
  for (i=0;i<RUNS;i++)
529 562
    {  
530
      /* Multiply A by X as Y <- A.X */
531 563
      cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,
532 564
		  dim,dim,dim,alpha,A,dim,B,dim,beta,C,dim);
533 565
      cblas_dgemm(CblasRowMajor,CblasTrans,CblasTrans,
......
538 570

  
539 571
  for (i=0;i<RUNS;i++)
540 572
    {  
541
      /* Multiply A by X as Y <- A.X */
542 573
      cblas_sgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,
543 574
		  dim,dim,dim,alpha,A,dim,B,dim,beta,C,dim);
544 575
      cblas_sgemm(CblasRowMajor,CblasTrans,CblasTrans,
......
619 650

  
620 651
  putchar('\n');
621 652

  
622
#ifdef CUBLAS
653
#ifdef CLBLAS
623 654
  double memoryIn,memoryOut;
624 655

  
625 656
  memoryIn=(double)((tv3.tv_sec-tv1.tv_sec) * 1000000L +	\
......
633 664

  
634 665
  printf("Duration of memory allocation : %2.10f s\n",memoryIn);
635 666
  printf("Duration of memory free : %2.10f s\n",memoryOut);
667
#elif CUBLAS
668
  double memoryIn,memoryOut;
669

  
670
  memoryIn=(double)((tv3.tv_sec-tv1.tv_sec) * 1000000L +	\
671
		    (tv3.tv_usec-tv1.tv_usec))/1000000.;  
672

  
673
  memoryOut=(double)((tv2.tv_sec-tv4.tv_sec) * 1000000L +	\
674
		    (tv2.tv_usec-tv4.tv_usec))/1000000.;  
675

  
676
  duration=(double)((tv4.tv_sec-tv3.tv_sec) * 1000000L +	\
677
		    (tv4.tv_usec-tv3.tv_usec))/1000000./RUNS;  
678

  
679
  printf("Duration of memory allocation : %2.10f s\n",memoryIn);
680
  printf("Duration of memory free : %2.10f s\n",memoryOut);
636 681
#else
637 682
  duration=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +	\
638 683
		    (tv2.tv_usec-tv1.tv_usec))/1000000./RUNS;  
......
657 702
  return 0;
658 703
}
659 704

  
705
#ifdef CLBLAS
706

  
707
int DelectOpenCLDevices() 
708
{
709
  /* */
710
  /* Not needed to import CL.h, already done in CLBLAS.h */
711

  
712
  int i, j;
713
  char* value;
714
  size_t valueSize;
715
  cl_uint platformCount;
716
  cl_platform_id* platforms;
717
  cl_uint deviceCount;
718
  cl_device_id* devices;
719
  cl_uint maxComputeUnits;
720
  cl_int maxWorkGroupSize;
721
  cl_int maxWorkItemSizes;
722
  cl_device_type dev_type;
723

  
724
  // get all platforms
725
  clGetPlatformIDs(0, NULL, &platformCount);
726
  platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
727
  clGetPlatformIDs(platformCount, platforms, NULL);
728

  
729

  
730
  printf("OpenCL statistics: %d platform(s) detected\n\n",platformCount);
731

  
732
  for (i = 0; i < platformCount; i++) {
733

  
734
    // get all devices
735
    clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount);
736
    devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
737
    clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL);
738

  
739
    // for each device print critical attributes
740
    for (j = 0; j < deviceCount; j++) {
741
      
742
      // print device name
743
      clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &valueSize);
744
      value = (char*) malloc(valueSize);
745
      clGetDeviceInfo(devices[j], CL_DEVICE_NAME, valueSize, value, NULL);
746
      printf("Device (%d,%d): %s\n",i, j, value);
747
      free(value);
748

  
749
      // print type device CPU/GPU/ACCELERATOR
750
      clGetDeviceInfo(devices[j], CL_DEVICE_TYPE, sizeof(dev_type), &dev_type, NULL);
751
      printf("\tDevice Type: ");
752
      if(dev_type & CL_DEVICE_TYPE_GPU)
753
	printf("CL_DEVICE_TYPE_GPU ");
754
      if(dev_type & CL_DEVICE_TYPE_CPU)
755
	printf("CL_DEVICE_TYPE_CPU ");
756
      if(dev_type & CL_DEVICE_TYPE_ACCELERATOR)
757
	printf("CL_DEVICE_TYPE_ACCELERATOR ");
758
      if(dev_type & CL_DEVICE_TYPE_DEFAULT)
759
	printf("CL_DEVICE_TYPE_DEFAULT ");
760
      printf("\n");
761

  
762
      // print device vendor
763
      clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, 0, NULL, &valueSize);
764
      value = (char*) malloc(valueSize);
765
      clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, valueSize, value, NULL);
766
      printf("\tDevice vendor: %s\n", value);
767
      free(value);
768

  
769
      // print hardware device version
770
      clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 0, NULL, &valueSize);
771
      value = (char*) malloc(valueSize);
772
      clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, valueSize, value, NULL);
773
      printf("\tHardware version: %s\n", value);
774
      free(value);
775

  
776
      // print software driver version
777
      clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, 0, NULL, &valueSize);
778
      value = (char*) malloc(valueSize);
779
      clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, valueSize, value, NULL);
780
      printf("\tSoftware version: %s\n", value);
781
      free(value);
782
      
783
      // print c version supported by compiler for device
784
      clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 0, NULL, &valueSize);
785
      value = (char*) malloc(valueSize);
786
      clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, valueSize, value, NULL);
787
      printf("\tOpenCL C version: %s\n", value);
788
      free(value);
789

  
790
      // print parallel compute units
791
      clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS,
792
		      sizeof(maxComputeUnits), &maxComputeUnits, NULL);
793
      printf("\tParallel compute units: %d\n", maxComputeUnits);
794
      
795
      // print max work group size
796
      clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_GROUP_SIZE,
797
		      sizeof(maxWorkGroupSize), &maxWorkGroupSize, NULL);
798
      printf("\tMaximum Work Group Size: %d\n", maxWorkGroupSize);
799
      
800
      // print max work items size
801
      clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_ITEM_SIZES,
802
		      sizeof(maxWorkItemSizes), &maxWorkItemSizes, NULL);
803
      printf("\tMaximum Work Item Sizes: %d\n", maxWorkItemSizes);
804
      
805
    }
806
    printf("\n");
807
    free(devices);
808
  }
809

  
810
  free(platforms);
811
  return 0;
812

  
813
}
814
#endif
815

  
660 816
int main(int argc,char **argv)
661 817
{
662 818
  if ((argc==1)||
663 819
      (strcmp(argv[1],"-h")==0)||
664 820
      (strcmp(argv[1],"--help")==0))
665 821
    {
822
#ifdef CLBLAS
666 823
      printf("\nPerforms a bench using BLAS library implementation:\n\n"
667 824
	     "\t#1 Size of square matrices \n"
825
	     "\t#2 Number of iterations \n"
826
	     "\t#3 OpenCL Plateform ID\n"
827
	     "\t#4 OpenCL Device ID\n\n");
828
      DelectOpenCLDevices();
829
#else
830
      printf("\nPerforms a bench using BLAS library implementation:\n\n"
831
	     "\t#1 Size of square matrices \n"
668 832
	     "\t#2 Number of iterations\n\n");
833
#endif
669 834
    }
670 835
  else if ((atoi(argv[1])>=2)&&
671 836
	   (atoi(argv[2])>=1))
672 837
    {
838
#ifdef CLBLAS
839
      MyPlatform=atoi(argv[3]);
840
      MyDevice=atoi(argv[4]);
841
#endif
673 842
      bench(atoi(argv[1]),atoi(argv[2]));
674 843
    }
675 844

  

Formats disponibles : Unified diff