You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

USAGE.md 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Notes on OpenBLAS usage
  2. ## Usage
  3. #### Program is Terminated. Because you tried to allocate too many memory regions
  4. In OpenBLAS, we mange a pool of memory buffers and allocate the number of
  5. buffers as the following.
  6. ```
  7. #define NUM_BUFFERS (MAX_CPU_NUMBER * 2)
  8. ```
  9. This error indicates that the program exceeded the number of buffers.
  10. Please build OpenBLAS with larger `NUM_THREADS`. For example, `make
  11. NUM_THREADS=32` or `make NUM_THREADS=64`. In `Makefile.system`, we will set
  12. `MAX_CPU_NUMBER=NUM_THREADS`.
  13. Despite its name, and due to the use of memory buffers in functions like SGEMM,
  14. the setting of NUM_THREADS can be relevant even for a single-threaded build
  15. of OpenBLAS, if such functions get called by multiple threads of a program
  16. that uses OpenBLAS. In some cases, the affected code may simply crash or throw
  17. a segmentation fault without displaying the above warning first.
  18. Note that the number of threads used at runtime can be altered to differ from the
  19. value NUM_THREADS was set to at build time. At runtime, the actual number of
  20. threads can be set anywhere from 1 to the build's NUM_THREADS (note however,
  21. that this does not change the number of memory buffers that will be allocated,
  22. which is set at build time). The number of threads for a process can be set by
  23. using the mechanisms described below.
  24. #### How can I use OpenBLAS in multi-threaded applications?
  25. If your application is already multi-threaded, it will conflict with OpenBLAS
  26. multi-threading. Thus, you must set OpenBLAS to use single thread in any of the
  27. following ways:
  28. * `export OPENBLAS_NUM_THREADS=1` in the environment variables.
  29. * Call `openblas_set_num_threads(1)` in the application on runtime.
  30. * Build OpenBLAS single thread version, e.g. `make USE_THREAD=0`
  31. If the application is parallelized by OpenMP, please use OpenBLAS built with
  32. `USE_OPENMP=1`
  33. #### How to choose TARGET manually at runtime when compiled with DYNAMIC_ARCH
  34. The environment variable which control the kernel selection is
  35. `OPENBLAS_CORETYPE` (see `driver/others/dynamic.c`) e.g. `export
  36. OPENBLAS_CORETYPE=Haswell` and the function `char* openblas_get_corename()`
  37. returns the used target.
  38. #### How could I disable OpenBLAS threading affinity on runtime?
  39. You can define the `OPENBLAS_MAIN_FREE` or `GOTOBLAS_MAIN_FREE` environment
  40. variable to disable threading affinity on runtime. For example, before the
  41. running,
  42. ```
  43. export OPENBLAS_MAIN_FREE=1
  44. ```
  45. Alternatively, you can disable affinity feature with enabling `NO_AFFINITY=1`
  46. in `Makefile.rule`.
  47. ## Linking with the library
  48. * Link with shared library
  49. `gcc -o test test.c -I /your_path/OpenBLAS/include/ -L/your_path/OpenBLAS/lib -lopenblas`
  50. If the library is multithreaded, please add `-lpthread`. If the library
  51. contains LAPACK functions, please add `-lgfortran` or other Fortran libs.
  52. * Link with static library
  53. `gcc -o test test.c /your/path/libopenblas.a`
  54. You can download `test.c` from https://gist.github.com/xianyi/5780018
  55. On Linux, if OpenBLAS was compiled with threading support (`USE_THREAD=1` by
  56. default), custom programs statically linked against `libopenblas.a` should also
  57. link with the pthread library e.g.:
  58. ```
  59. gcc -static -I/opt/OpenBLAS/include -L/opt/OpenBLAS/lib -o my_program my_program.c -lopenblas -lpthread
  60. ```
  61. Failing to add the `-lpthread` flag will cause errors such as:
  62. ```
  63. /opt/OpenBLAS/libopenblas.a(memory.o): In function `_touch_memory':
  64. memory.c:(.text+0x15): undefined reference to `pthread_mutex_lock'
  65. memory.c:(.text+0x41): undefined reference to `pthread_mutex_unlock'
  66. ...
  67. ```
  68. ## Code examples
  69. #### Call CBLAS interface
  70. This example shows calling cblas_dgemm in C. https://gist.github.com/xianyi/6930656
  71. ```
  72. #include <cblas.h>
  73. #include <stdio.h>
  74. void main()
  75. {
  76. int i=0;
  77. double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
  78. double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
  79. double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5};
  80. cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,3,3,2,1,A, 3, B, 3,2,C,3);
  81. for(i=0; i<9; i++)
  82. printf("%lf ", C[i]);
  83. printf("\n");
  84. }
  85. ```
  86. `gcc -o test_cblas_open test_cblas_dgemm.c -I /your_path/OpenBLAS/include/ -L/your_path/OpenBLAS/lib -lopenblas -lpthread -lgfortran`
  87. #### Call BLAS Fortran interface
  88. This example shows calling dgemm Fortran interface in C. https://gist.github.com/xianyi/5780018
  89. ```
  90. #include "stdio.h"
  91. #include "stdlib.h"
  92. #include "sys/time.h"
  93. #include "time.h"
  94. extern void dgemm_(char*, char*, int*, int*,int*, double*, double*, int*, double*, int*, double*, double*, int*);
  95. int main(int argc, char* argv[])
  96. {
  97. int i;
  98. printf("test!\n");
  99. if(argc<4){
  100. printf("Input Error\n");
  101. return 1;
  102. }
  103. int m = atoi(argv[1]);
  104. int n = atoi(argv[2]);
  105. int k = atoi(argv[3]);
  106. int sizeofa = m * k;
  107. int sizeofb = k * n;
  108. int sizeofc = m * n;
  109. char ta = 'N';
  110. char tb = 'N';
  111. double alpha = 1.2;
  112. double beta = 0.001;
  113. struct timeval start,finish;
  114. double duration;
  115. double* A = (double*)malloc(sizeof(double) * sizeofa);
  116. double* B = (double*)malloc(sizeof(double) * sizeofb);
  117. double* C = (double*)malloc(sizeof(double) * sizeofc);
  118. srand((unsigned)time(NULL));
  119. for (i=0; i<sizeofa; i++)
  120. A[i] = i%3+1;//(rand()%100)/10.0;
  121. for (i=0; i<sizeofb; i++)
  122. B[i] = i%3+1;//(rand()%100)/10.0;
  123. for (i=0; i<sizeofc; i++)
  124. C[i] = i%3+1;//(rand()%100)/10.0;
  125. //#if 0
  126. printf("m=%d,n=%d,k=%d,alpha=%lf,beta=%lf,sizeofc=%d\n",m,n,k,alpha,beta,sizeofc);
  127. gettimeofday(&start, NULL);
  128. dgemm_(&ta, &tb, &m, &n, &k, &alpha, A, &m, B, &k, &beta, C, &m);
  129. gettimeofday(&finish, NULL);
  130. duration = ((double)(finish.tv_sec-start.tv_sec)*1000000 + (double)(finish.tv_usec-start.tv_usec)) / 1000000;
  131. double gflops = 2.0 * m *n*k;
  132. gflops = gflops/duration*1.0e-6;
  133. FILE *fp;
  134. fp = fopen("timeDGEMM.txt", "a");
  135. fprintf(fp, "%dx%dx%d\t%lf s\t%lf MFLOPS\n", m, n, k, duration, gflops);
  136. fclose(fp);
  137. free(A);
  138. free(B);
  139. free(C);
  140. return 0;
  141. }
  142. ```
  143. ` gcc -o time_dgemm time_dgemm.c /your/path/libopenblas.a`
  144. ` ./time_dgemm <m> <n> <k> `
  145. ## Troubleshooting
  146. * Please read [Faq](https://github.com/xianyi/OpenBLAS/wiki/Faq) at first.
  147. * Please use gcc version 4.6 and above to compile Sandy Bridge AVX kernels on Linux/MingW/BSD.
  148. * Please use Clang version 3.1 and above to compile the library on Sandy Bridge microarchitecture. The Clang 3.0 will generate the wrong AVX binary code.
  149. * The number of CPUs/Cores should less than or equal to 256. On Linux x86_64(amd64), there is experimental support for up to 1024 CPUs/Cores and 128 numa nodes if you build the library with BIGNUMA=1.
  150. * OpenBLAS does not set processor affinity by default. On Linux, you can enable processor affinity by commenting the line NO_AFFINITY=1 in Makefile.rule. But this may cause [the conflict with R parallel](https://stat.ethz.ch/pipermail/r-sig-hpc/2012-April/001348.html).
  151. * On Loongson 3A. make test would be failed because of pthread_create error. The error code is EAGAIN. However, it will be OK when you run the same testcase on shell.
  152. ## BLAS reference manual
  153. If you want to understand every BLAS function and definition, please read
  154. [Intel MKL reference manual](https://software.intel.com/sites/products/documentation/doclib/iss/2013/mkl/mklman/GUID-F7ED9FB8-6663-4F44-A62B-61B63C4F0491.htm)
  155. or [netlib.org](http://netlib.org/blas/)
  156. Here are [OpenBLAS extension functions](https://github.com/xianyi/OpenBLAS/wiki/OpenBLAS-Extensions)
  157. ## How to reference OpenBLAS.
  158. You can reference our [papers](https://github.com/xianyi/OpenBLAS/wiki/publications).
  159. Alternatively, you can cite the OpenBLAS homepage http://www.openblas.net directly.