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.

memory.c 84 kB

Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
4 years ago
4 years ago
7 years ago
7 years ago
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
9 years ago
9 years ago
9 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483
  1. /*****************************************************************************
  2. Copyright (c) 2011-2014, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. /*********************************************************************/
  29. /* Copyright 2009, 2010 The University of Texas at Austin. */
  30. /* All rights reserved. */
  31. /* */
  32. /* Redistribution and use in source and binary forms, with or */
  33. /* without modification, are permitted provided that the following */
  34. /* conditions are met: */
  35. /* */
  36. /* 1. Redistributions of source code must retain the above */
  37. /* copyright notice, this list of conditions and the following */
  38. /* disclaimer. */
  39. /* */
  40. /* 2. Redistributions in binary form must reproduce the above */
  41. /* copyright notice, this list of conditions and the following */
  42. /* disclaimer in the documentation and/or other materials */
  43. /* provided with the distribution. */
  44. /* */
  45. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  46. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  47. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  48. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  49. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  50. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  51. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  52. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  53. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  54. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  55. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  56. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  57. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  58. /* POSSIBILITY OF SUCH DAMAGE. */
  59. /* */
  60. /* The views and conclusions contained in the software and */
  61. /* documentation are those of the authors and should not be */
  62. /* interpreted as representing official policies, either expressed */
  63. /* or implied, of The University of Texas at Austin. */
  64. /*********************************************************************/
  65. //#undef DEBUG
  66. #include "common.h"
  67. #ifndef likely
  68. #ifdef __GNUC__
  69. #define likely(x) __builtin_expect(!!(x), 1)
  70. #define unlikely(x) __builtin_expect(!!(x), 0)
  71. #else
  72. #define likely(x) (x)
  73. #define unlikely(x) (x)
  74. #endif
  75. #endif
  76. #if defined(USE_TLS) && defined(SMP)
  77. #define COMPILE_TLS
  78. #if USE_TLS != 1
  79. #undef COMPILE_TLS
  80. #endif
  81. #if defined(__GLIBC_PREREQ)
  82. #if !__GLIBC_PREREQ(2,20)
  83. #undef COMPILE_TLS
  84. #endif
  85. #endif
  86. #endif
  87. /* Memory buffer must fit two matrix subblocks of maximal size */
  88. #define XSTR(x) STR(x)
  89. #define STR(x) #x
  90. #if BUFFER_SIZE < (SGEMM_DEFAULT_P * SGEMM_DEFAULT_Q * 4 * 2) || \
  91. BUFFER_SIZE < (SGEMM_DEFAULT_P * SGEMM_DEFAULT_R * 4 * 2) || \
  92. BUFFER_SIZE < (SGEMM_DEFAULT_R * SGEMM_DEFAULT_Q * 4 * 2)
  93. #warning BUFFER_SIZE is too small for P, Q, and R of SGEMM - large calculations may crash !
  94. #endif
  95. #if BUFFER_SIZE < (DGEMM_DEFAULT_P * DGEMM_DEFAULT_Q * 8 * 2) || \
  96. BUFFER_SIZE < (DGEMM_DEFAULT_P * DGEMM_DEFAULT_R * 8 * 2) || \
  97. BUFFER_SIZE < (DGEMM_DEFAULT_R * DGEMM_DEFAULT_Q * 8 * 2)
  98. #warning BUFFER_SIZE is too small for P, Q, and R of DGEMM - large calculations may crash !
  99. #endif
  100. #if BUFFER_SIZE < (CGEMM_DEFAULT_P * CGEMM_DEFAULT_Q * 8 * 2) || \
  101. BUFFER_SIZE < (CGEMM_DEFAULT_P * CGEMM_DEFAULT_R * 8 * 2) || \
  102. BUFFER_SIZE < (CGEMM_DEFAULT_R * CGEMM_DEFAULT_Q * 8 * 2)
  103. #warning BUFFER_SIZE is too small for P, Q, and R of CGEMM - large calculations may crash !
  104. #endif
  105. #if BUFFER_SIZE < (ZGEMM_DEFAULT_P * ZGEMM_DEFAULT_Q * 16 * 2) || \
  106. BUFFER_SIZE < (ZGEMM_DEFAULT_P * ZGEMM_DEFAULT_R * 16 * 2) || \
  107. BUFFER_SIZE < (ZGEMM_DEFAULT_R * ZGEMM_DEFAULT_Q * 16 * 2)
  108. #warning BUFFER_SIZE is too small for P, Q, and R of ZGEMM - large calculations may crash !
  109. #endif
  110. #if defined(COMPILE_TLS)
  111. #include <errno.h>
  112. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  113. #define ALLOC_WINDOWS
  114. #ifndef MEM_LARGE_PAGES
  115. #define MEM_LARGE_PAGES 0x20000000
  116. #endif
  117. #else
  118. #define ALLOC_MMAP
  119. #define ALLOC_MALLOC
  120. #endif
  121. #include <stdlib.h>
  122. #include <stdio.h>
  123. #include <fcntl.h>
  124. #if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
  125. #include <sys/mman.h>
  126. #ifndef NO_SYSV_IPC
  127. #include <sys/shm.h>
  128. #endif
  129. #include <sys/ipc.h>
  130. #endif
  131. #include <sys/types.h>
  132. #ifdef OS_LINUX
  133. #include <sys/sysinfo.h>
  134. #include <sched.h>
  135. #include <errno.h>
  136. #include <linux/unistd.h>
  137. #include <sys/syscall.h>
  138. #include <sys/time.h>
  139. #include <sys/resource.h>
  140. #endif
  141. #ifdef OS_HAIKU
  142. #include <unistd.h>
  143. #endif
  144. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
  145. #include <sys/sysctl.h>
  146. #include <sys/resource.h>
  147. #endif
  148. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  149. #include <conio.h>
  150. #undef printf
  151. #define printf _cprintf
  152. #endif
  153. #ifdef OS_LINUX
  154. #ifndef MPOL_PREFERRED
  155. #define MPOL_PREFERRED 1
  156. #endif
  157. #endif
  158. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  159. #define NO_WARMUP
  160. #endif
  161. #ifndef SHM_HUGETLB
  162. #define SHM_HUGETLB 04000
  163. #endif
  164. #ifndef FIXED_PAGESIZE
  165. #define FIXED_PAGESIZE 4096
  166. #endif
  167. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  168. #if defined(_MSC_VER) && !defined(__clang__)
  169. #define CONSTRUCTOR __cdecl
  170. #define DESTRUCTOR __cdecl
  171. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  172. #define CONSTRUCTOR __attribute__ ((constructor))
  173. #define DESTRUCTOR __attribute__ ((destructor))
  174. #elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
  175. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  176. #define DESTRUCTOR __attribute__ ((destructor(101)))
  177. #else
  178. #define CONSTRUCTOR __attribute__ ((constructor))
  179. #define DESTRUCTOR __attribute__ ((destructor))
  180. #endif
  181. #ifdef DYNAMIC_ARCH
  182. gotoblas_t *gotoblas = NULL;
  183. #endif
  184. extern void openblas_warning(int verbose, const char * msg);
  185. #ifndef SMP
  186. #define blas_cpu_number 1
  187. #define blas_num_threads 1
  188. /* Dummy Function */
  189. int goto_get_num_procs (void) { return 1;};
  190. void goto_set_num_threads(int num_threads) {};
  191. #else
  192. #if defined(OS_LINUX) || defined(OS_SUNOS)
  193. #ifndef NO_AFFINITY
  194. int get_num_procs(void);
  195. #else
  196. int get_num_procs(void) {
  197. static int nums = 0;
  198. #if defined(__GLIBC_PREREQ)
  199. cpu_set_t cpuset,*cpusetp;
  200. size_t size;
  201. int ret;
  202. #if !__GLIBC_PREREQ(2, 7)
  203. int i;
  204. #if !__GLIBC_PREREQ(2, 6)
  205. int n;
  206. #endif
  207. #endif
  208. #endif
  209. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  210. #if !defined(OS_LINUX)
  211. return nums;
  212. #endif
  213. #if !defined(__GLIBC_PREREQ)
  214. return nums;
  215. #else
  216. #if !__GLIBC_PREREQ(2, 3)
  217. return nums;
  218. #endif
  219. #if !__GLIBC_PREREQ(2, 7)
  220. ret = sched_getaffinity(0,sizeof(cpuset), &cpuset);
  221. if (ret!=0) return nums;
  222. n=0;
  223. #if !__GLIBC_PREREQ(2, 6)
  224. for (i=0;i<nums;i++)
  225. if (CPU_ISSET(i,&cpuset)) n++;
  226. nums=n;
  227. #else
  228. nums = CPU_COUNT(sizeof(cpuset),&cpuset);
  229. #endif
  230. return nums;
  231. #else
  232. if (nums >= CPU_SETSIZE) {
  233. cpusetp = CPU_ALLOC(nums);
  234. if (cpusetp == NULL) {
  235. return nums;
  236. }
  237. size = CPU_ALLOC_SIZE(nums);
  238. ret = sched_getaffinity(0,size,cpusetp);
  239. if (ret!=0) {
  240. CPU_FREE(cpusetp);
  241. return nums;
  242. }
  243. ret = CPU_COUNT_S(size,cpusetp);
  244. if (ret > 0 && ret < nums) nums = ret;
  245. CPU_FREE(cpusetp);
  246. return nums;
  247. } else {
  248. ret = sched_getaffinity(0,sizeof(cpuset),&cpuset);
  249. if (ret!=0) {
  250. return nums;
  251. }
  252. ret = CPU_COUNT(&cpuset);
  253. if (ret > 0 && ret < nums) nums = ret;
  254. return nums;
  255. }
  256. #endif
  257. #endif
  258. }
  259. #endif
  260. #endif
  261. #ifdef OS_ANDROID
  262. int get_num_procs(void) {
  263. static int nums = 0;
  264. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  265. return nums;
  266. }
  267. #endif
  268. #ifdef OS_HAIKU
  269. int get_num_procs(void) {
  270. static int nums = 0;
  271. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  272. return nums;
  273. }
  274. #endif
  275. #ifdef OS_AIX
  276. int get_num_procs(void) {
  277. static int nums = 0;
  278. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  279. return nums;
  280. }
  281. #endif
  282. #ifdef OS_WINDOWS
  283. int get_num_procs(void) {
  284. static int nums = 0;
  285. if (nums == 0) {
  286. SYSTEM_INFO sysinfo;
  287. GetSystemInfo(&sysinfo);
  288. nums = sysinfo.dwNumberOfProcessors;
  289. }
  290. return nums;
  291. }
  292. #endif
  293. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY)
  294. int get_num_procs(void) {
  295. static int nums = 0;
  296. int m[2];
  297. size_t len;
  298. if (nums == 0) {
  299. m[0] = CTL_HW;
  300. m[1] = HW_NCPU;
  301. len = sizeof(int);
  302. sysctl(m, 2, &nums, &len, NULL, 0);
  303. }
  304. return nums;
  305. }
  306. #endif
  307. #if defined(OS_DARWIN)
  308. int get_num_procs(void) {
  309. static int nums = 0;
  310. size_t len;
  311. if (nums == 0){
  312. len = sizeof(int);
  313. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  314. }
  315. return nums;
  316. }
  317. /*
  318. void set_stack_limit(int limitMB){
  319. int result=0;
  320. struct rlimit rl;
  321. rlim_t StackSize;
  322. StackSize=limitMB*1024*1024;
  323. result=getrlimit(RLIMIT_STACK, &rl);
  324. if(result==0){
  325. if(rl.rlim_cur < StackSize){
  326. rl.rlim_cur=StackSize;
  327. result=setrlimit(RLIMIT_STACK, &rl);
  328. if(result !=0){
  329. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  330. }
  331. }
  332. }
  333. }
  334. */
  335. #endif
  336. /*
  337. OpenBLAS uses the numbers of CPU cores in multithreading.
  338. It can be set by openblas_set_num_threads(int num_threads);
  339. */
  340. int blas_cpu_number = 0;
  341. /*
  342. The numbers of threads in the thread pool.
  343. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  344. */
  345. int blas_num_threads = 0;
  346. int goto_get_num_procs (void) {
  347. return blas_cpu_number;
  348. }
  349. static void blas_memory_init();
  350. void openblas_fork_handler()
  351. {
  352. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  353. // built with "make USE_OPENMP=0".
  354. // Hanging can still happen when OpenBLAS is built against the libgomp
  355. // implementation of OpenMP. The problem is tracked at:
  356. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  357. // In the mean time build with USE_OPENMP=0 or link against another
  358. // implementation of OpenMP.
  359. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  360. int err;
  361. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, blas_memory_init);
  362. if(err != 0)
  363. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  364. #endif
  365. }
  366. extern int openblas_num_threads_env();
  367. extern int openblas_goto_num_threads_env();
  368. extern int openblas_omp_num_threads_env();
  369. int blas_get_cpu_number(void){
  370. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_HAIKU)
  371. int max_num;
  372. #endif
  373. int blas_goto_num = 0;
  374. int blas_omp_num = 0;
  375. if (blas_num_threads) return blas_num_threads;
  376. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_HAIKU)
  377. max_num = get_num_procs();
  378. #endif
  379. // blas_goto_num = 0;
  380. #ifndef USE_OPENMP_UNUSED
  381. blas_goto_num=openblas_num_threads_env();
  382. if (blas_goto_num < 0) blas_goto_num = 0;
  383. if (blas_goto_num == 0) {
  384. blas_goto_num=openblas_goto_num_threads_env();
  385. if (blas_goto_num < 0) blas_goto_num = 0;
  386. }
  387. #endif
  388. // blas_omp_num = 0;
  389. blas_omp_num=openblas_omp_num_threads_env();
  390. if (blas_omp_num < 0) blas_omp_num = 0;
  391. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  392. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  393. else blas_num_threads = MAX_CPU_NUMBER;
  394. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_HAIKU)
  395. if (blas_num_threads > max_num) blas_num_threads = max_num;
  396. #endif
  397. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  398. #ifdef DEBUG
  399. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  400. #endif
  401. blas_cpu_number = blas_num_threads;
  402. return blas_num_threads;
  403. }
  404. #endif
  405. int openblas_get_num_procs(void) {
  406. #ifndef SMP
  407. return 1;
  408. #else
  409. return get_num_procs();
  410. #endif
  411. }
  412. int openblas_get_num_threads(void) {
  413. #ifndef SMP
  414. return 1;
  415. #else
  416. // init blas_cpu_number if needed
  417. blas_get_cpu_number();
  418. return blas_cpu_number;
  419. #endif
  420. }
  421. int hugetlb_allocated = 0;
  422. #if defined(OS_WINDOWS)
  423. #define LIKELY_ONE(x) (x)
  424. #else
  425. #define LIKELY_ONE(x) (__builtin_expect(x, 1))
  426. #endif
  427. /* Stores information about the allocation and how to release it */
  428. struct alloc_t {
  429. /* Whether this allocation is being used */
  430. int used;
  431. /* Any special attributes needed when releasing this allocation */
  432. int attr;
  433. /* Function that can properly release this memory */
  434. void (*release_func)(struct alloc_t *);
  435. /* Pad to 64-byte alignment */
  436. char pad[64 - 2 * sizeof(int) - sizeof(void(*))];
  437. };
  438. /* Convenience macros for storing release funcs */
  439. #define STORE_RELEASE_FUNC(address, func) \
  440. if (address != (void *)-1) { \
  441. struct alloc_t *alloc_info = (struct alloc_t *)address; \
  442. alloc_info->release_func = func; \
  443. }
  444. #define STORE_RELEASE_FUNC_WITH_ATTR(address, func, attr) \
  445. if (address != (void *)-1) { \
  446. struct alloc_t *alloc_info = (struct alloc_t *)address; \
  447. alloc_info->release_func = func; \
  448. alloc_info->attr = attr; \
  449. }
  450. /* The number of bytes that will be allocated for each buffer. When allocating
  451. memory, we store an alloc_t followed by the actual buffer memory. This means
  452. that each allocation always has its associated alloc_t, without the need
  453. for an auxiliary tracking structure. */
  454. static const int allocation_block_size = BUFFER_SIZE + sizeof(struct alloc_t);
  455. #if defined(SMP)
  456. # if defined(OS_WINDOWS)
  457. static DWORD local_storage_key = 0;
  458. DWORD lsk;
  459. # else
  460. static pthread_key_t local_storage_key = 0;
  461. pthread_key_t lsk;
  462. # endif /* defined(OS_WINDOWS) */
  463. #endif /* defined(SMP) */
  464. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  465. static int hot_alloc = 0;
  466. #endif
  467. /* Global lock for memory allocation */
  468. #if defined(USE_PTHREAD_LOCK)
  469. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  470. #elif defined(USE_PTHREAD_SPINLOCK)
  471. static pthread_spinlock_t alloc_lock = 0;
  472. #else
  473. static BLASULONG alloc_lock = 0UL;
  474. #endif
  475. #if defined(USE_PTHREAD_LOCK)
  476. static pthread_mutex_t key_lock = PTHREAD_MUTEX_INITIALIZER;
  477. #elif defined(USE_PTHREAD_SPINLOCK)
  478. static pthread_spinlock_t key_lock = 0;
  479. #else
  480. static BLASULONG key_lock = 0UL;
  481. #endif
  482. /* Returns a pointer to the start of the per-thread memory allocation data */
  483. static __inline struct alloc_t ** get_memory_table() {
  484. #if defined(SMP)
  485. LOCK_COMMAND(&key_lock);
  486. lsk=local_storage_key;
  487. UNLOCK_COMMAND(&key_lock);
  488. if (!lsk) {
  489. blas_memory_init();
  490. }
  491. # if defined(OS_WINDOWS)
  492. struct alloc_t ** local_memory_table = (struct alloc_t **)TlsGetValue(local_storage_key);
  493. # else
  494. struct alloc_t ** local_memory_table = (struct alloc_t **)pthread_getspecific(local_storage_key);
  495. # endif /* defined(OS_WINDOWS) */
  496. #else
  497. static struct alloc_t ** local_memory_table = NULL;
  498. #endif /* defined(SMP) */
  499. #if defined (SMP)
  500. LOCK_COMMAND(&key_lock);
  501. lsk=local_storage_key;
  502. UNLOCK_COMMAND(&key_lock);
  503. if (lsk && !local_memory_table) {
  504. #else
  505. if (!local_memory_table) {
  506. #endif /* defined(SMP) */
  507. local_memory_table = (struct alloc_t **)malloc(sizeof(struct alloc_t *) * NUM_BUFFERS);
  508. memset(local_memory_table, 0, sizeof(struct alloc_t *) * NUM_BUFFERS);
  509. #if defined(SMP)
  510. # if defined(OS_WINDOWS)
  511. LOCK_COMMAND(&key_lock);
  512. TlsSetValue(local_storage_key, (void*)local_memory_table);
  513. UNLOCK_COMMAND(&key_lock);
  514. # else
  515. LOCK_COMMAND(&key_lock);
  516. pthread_setspecific(local_storage_key, (void*)local_memory_table);
  517. UNLOCK_COMMAND(&key_lock);
  518. # endif /* defined(OS_WINDOWS) */
  519. #endif /* defined(SMP) */
  520. }
  521. return local_memory_table;
  522. }
  523. #ifdef ALLOC_MMAP
  524. static void alloc_mmap_free(struct alloc_t *alloc_info){
  525. if (munmap(alloc_info, allocation_block_size)) {
  526. printf("OpenBLAS : munmap failed\n");
  527. }
  528. }
  529. #ifdef NO_WARMUP
  530. static void *alloc_mmap(void *address){
  531. void *map_address;
  532. if (address){
  533. map_address = mmap(address,
  534. allocation_block_size,
  535. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  536. } else {
  537. map_address = mmap(address,
  538. allocation_block_size,
  539. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  540. }
  541. STORE_RELEASE_FUNC(map_address, alloc_mmap_free);
  542. #ifdef OS_LINUX
  543. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  544. #endif
  545. return map_address;
  546. }
  547. #else
  548. #define BENCH_ITERATION 4
  549. #define SCALING 2
  550. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  551. BLASULONG original, *p;
  552. BLASULONG start, stop, min;
  553. int iter, i, count;
  554. min = (BLASULONG)-1;
  555. original = *(BLASULONG *)(address + size - PAGESIZE);
  556. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  557. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  558. p = (BLASULONG *)address;
  559. count = size / PAGESIZE;
  560. start = rpcc();
  561. for (i = 0; i < count; i ++) {
  562. p = (BLASULONG *)(*p);
  563. }
  564. stop = rpcc();
  565. if (min > stop - start) min = stop - start;
  566. }
  567. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  568. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  569. return min;
  570. }
  571. static void *alloc_mmap(void *address){
  572. void *map_address, *best_address;
  573. BLASULONG best, start, current, original;
  574. BLASULONG allocsize;
  575. if (address){
  576. /* Just give up use advanced operation */
  577. map_address = mmap(address, allocation_block_size, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  578. #ifdef OS_LINUX
  579. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  580. #endif
  581. } else {
  582. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  583. if (hot_alloc == 0) {
  584. map_address = mmap(NULL, allocation_block_size, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  585. #ifdef OS_LINUX
  586. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  587. #endif
  588. } else {
  589. #endif
  590. map_address = mmap(NULL, allocation_block_size * SCALING,
  591. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  592. if (map_address != (void *)-1) {
  593. #ifdef OS_LINUX
  594. #ifdef DEBUG
  595. int ret=0;
  596. ret=my_mbind(map_address, allocation_block_size * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  597. if(ret==-1){
  598. int errsv=errno;
  599. perror("OpenBLAS alloc_mmap:");
  600. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  601. }
  602. #else
  603. my_mbind(map_address, allocation_block_size * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  604. #endif
  605. #endif
  606. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  607. start = (BLASULONG)map_address;
  608. current = (SCALING - 1) * allocation_block_size;
  609. original = current;
  610. while(current > 0 && current <= original) {
  611. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  612. start += PAGESIZE;
  613. current -= PAGESIZE;
  614. }
  615. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  616. start = (BLASULONG)map_address;
  617. best = (BLASULONG)-1;
  618. best_address = map_address;
  619. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * allocation_block_size)) {
  620. current = run_bench(start, allocsize);
  621. if (best > current) {
  622. best = current;
  623. best_address = (void *)start;
  624. }
  625. start += PAGESIZE;
  626. }
  627. if ((BLASULONG)best_address > (BLASULONG)map_address)
  628. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  629. munmap((void *)((BLASULONG)best_address + allocation_block_size), (SCALING - 1) * allocation_block_size + (BLASULONG)map_address - (BLASULONG)best_address);
  630. map_address = best_address;
  631. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  632. hot_alloc = 2;
  633. #endif
  634. }
  635. }
  636. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  637. }
  638. #endif
  639. STORE_RELEASE_FUNC(map_address, alloc_mmap_free);
  640. return map_address;
  641. }
  642. #endif
  643. #endif
  644. #ifdef ALLOC_MALLOC
  645. static void alloc_malloc_free(struct alloc_t *alloc_info){
  646. free(alloc_info);
  647. }
  648. static void *alloc_malloc(void *address){
  649. void *map_address;
  650. map_address = (void *)malloc(allocation_block_size + FIXED_PAGESIZE);
  651. if (map_address == (void *)NULL) map_address = (void *)-1;
  652. STORE_RELEASE_FUNC(map_address, alloc_malloc_free);
  653. return map_address;
  654. }
  655. #endif
  656. #ifdef ALLOC_QALLOC
  657. void *qalloc(int flags, size_t bytes);
  658. void *qfree (void *address);
  659. #define QNONCACHE 0x1
  660. #define QCOMMS 0x2
  661. #define QFAST 0x4
  662. static void alloc_qalloc_free(struct alloc_t *alloc_info){
  663. qfree(alloc_info);
  664. }
  665. static void *alloc_qalloc(void *address){
  666. void *map_address;
  667. map_address = (void *)qalloc(QCOMMS | QFAST, allocation_block_size + FIXED_PAGESIZE);
  668. if (map_address == (void *)NULL) map_address = (void *)-1;
  669. STORE_RELEASE_FUNC(map_address, alloc_qalloc_free);
  670. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  671. }
  672. #endif
  673. #ifdef ALLOC_WINDOWS
  674. static void alloc_windows_free(struct alloc_t *alloc_info){
  675. VirtualFree(alloc_info, 0, MEM_RELEASE);
  676. }
  677. static void *alloc_windows(void *address){
  678. void *map_address;
  679. map_address = VirtualAlloc(address,
  680. allocation_block_size,
  681. MEM_RESERVE | MEM_COMMIT,
  682. PAGE_READWRITE);
  683. if (map_address == (void *)NULL) map_address = (void *)-1;
  684. STORE_RELEASE_FUNC(map_address, alloc_windows_free);
  685. return map_address;
  686. }
  687. #endif
  688. #ifdef ALLOC_DEVICEDRIVER
  689. #ifndef DEVICEDRIVER_NAME
  690. #define DEVICEDRIVER_NAME "/dev/mapper"
  691. #endif
  692. static void alloc_devicedirver_free(struct alloc_t *alloc_info){
  693. int attr = alloc_info -> attr;
  694. if (munmap(address, allocation_block_size)) {
  695. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  696. }
  697. if (close(attr)) {
  698. printf("OpenBLAS : Bugphysarea close failed.\n");
  699. }
  700. }
  701. static void *alloc_devicedirver(void *address){
  702. int fd;
  703. void *map_address;
  704. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  705. return (void *)-1;
  706. }
  707. map_address = mmap(address, allocation_block_size,
  708. PROT_READ | PROT_WRITE,
  709. MAP_FILE | MAP_SHARED,
  710. fd, 0);
  711. STORE_RELEASE_FUNC_WITH_ATTR(map_address, alloc_devicedirver_free, fd);
  712. return map_address;
  713. }
  714. #endif
  715. #ifdef ALLOC_SHM
  716. static void alloc_shm_free(struct alloc_t *alloc_info){
  717. if (shmdt(alloc_info)) {
  718. printf("OpenBLAS : Shared memory unmap failed.\n");
  719. }
  720. }
  721. static void *alloc_shm(void *address){
  722. void *map_address;
  723. int shmid;
  724. shmid = shmget(IPC_PRIVATE, allocation_block_size,IPC_CREAT | 0600);
  725. map_address = (void *)shmat(shmid, address, 0);
  726. if (map_address != (void *)-1){
  727. #ifdef OS_LINUX
  728. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  729. #endif
  730. shmctl(shmid, IPC_RMID, 0);
  731. struct alloc_t *alloc_info = (struct alloc_t *)map_address;
  732. alloc_info->release_func = alloc_shm_free;
  733. alloc_info->attr = shmid;
  734. }
  735. return map_address;
  736. }
  737. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  738. static void alloc_hugetlb_free(struct alloc_t *alloc_info){
  739. #if defined(OS_LINUX) || defined(OS_AIX)
  740. if (shmdt(alloc_info)) {
  741. printf("OpenBLAS : Hugepage unmap failed.\n");
  742. }
  743. #endif
  744. #ifdef __sun__
  745. munmap(alloc_info, allocation_block_size);
  746. #endif
  747. #ifdef OS_WINDOWS
  748. VirtualFree(alloc_info, 0, MEM_LARGE_PAGES | MEM_RELEASE);
  749. #endif
  750. }
  751. static void *alloc_hugetlb(void *address){
  752. void *map_address = (void *)-1;
  753. #if defined(OS_LINUX) || defined(OS_AIX)
  754. int shmid;
  755. shmid = shmget(IPC_PRIVATE, allocation_block_size,
  756. #ifdef OS_LINUX
  757. SHM_HUGETLB |
  758. #endif
  759. #ifdef OS_AIX
  760. SHM_LGPAGE | SHM_PIN |
  761. #endif
  762. IPC_CREAT | SHM_R | SHM_W);
  763. if (shmid != -1) {
  764. map_address = (void *)shmat(shmid, address, SHM_RND);
  765. #ifdef OS_LINUX
  766. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  767. #endif
  768. if (map_address != (void *)-1){
  769. shmctl(shmid, IPC_RMID, 0);
  770. }
  771. }
  772. #endif
  773. #ifdef __sun__
  774. struct memcntl_mha mha;
  775. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  776. mha.mha_flags = 0;
  777. mha.mha_pagesize = HUGE_PAGESIZE;
  778. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  779. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, allocation_block_size);
  780. #endif
  781. #ifdef OS_WINDOWS
  782. HANDLE hToken;
  783. TOKEN_PRIVILEGES tp;
  784. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  785. tp.PrivilegeCount = 1;
  786. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  787. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  788. CloseHandle(hToken);
  789. return (void*)-1;
  790. }
  791. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  792. CloseHandle(hToken);
  793. return (void*)-1;
  794. }
  795. map_address = (void *)VirtualAlloc(address,
  796. allocation_block_size,
  797. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  798. PAGE_READWRITE);
  799. tp.Privileges[0].Attributes = 0;
  800. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  801. if (map_address == (void *)NULL) map_address = (void *)-1;
  802. #endif
  803. STORE_RELEASE_FUNC(map_address, alloc_hugetlb_free);
  804. return map_address;
  805. }
  806. #endif
  807. #endif
  808. #ifdef ALLOC_HUGETLBFILE
  809. static int hugetlb_pid = 0;
  810. static void alloc_hugetlbfile_free(struct alloc_t *alloc_info){
  811. int attr = alloc_info -> attr;
  812. if (munmap(alloc_info, allocation_block_size)) {
  813. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  814. }
  815. if (close(attr)) {
  816. printf("OpenBLAS : HugeTLBfs close failed.\n");
  817. }
  818. }
  819. static void *alloc_hugetlbfile(void *address){
  820. void *map_address = (void *)-1;
  821. int fd;
  822. char filename[64];
  823. if (!hugetlb_pid) hugetlb_pid = getpid();
  824. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  825. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  826. return (void *)-1;
  827. }
  828. unlink(filename);
  829. map_address = mmap(address, allocation_block_size,
  830. PROT_READ | PROT_WRITE,
  831. MAP_SHARED,
  832. fd, 0);
  833. STORE_RELEASE_FUNC_WITH_ATTR(map_address, alloc_hugetlbfile_free, fd);
  834. return map_address;
  835. }
  836. #endif
  837. #ifdef SEEK_ADDRESS
  838. static BLASULONG base_address = 0UL;
  839. #else
  840. static BLASULONG base_address = BASE_ADDRESS;
  841. #endif
  842. #ifdef HAVE_C11
  843. static _Atomic int memory_initialized = 0;
  844. #else
  845. static volatile int memory_initialized = 0;
  846. #endif
  847. /* Memory allocation routine */
  848. /* procpos ... indicates where it comes from */
  849. /* 0 : Level 3 functions */
  850. /* 1 : Level 2 functions */
  851. /* 2 : Thread */
  852. static void blas_memory_cleanup(void* ptr){
  853. if (ptr) {
  854. struct alloc_t ** table = (struct alloc_t **)ptr;
  855. int pos;
  856. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  857. struct alloc_t *alloc_info = table[pos];
  858. if (alloc_info) {
  859. alloc_info->release_func(alloc_info);
  860. table[pos] = (void *)0;
  861. }
  862. }
  863. free(table);
  864. }
  865. }
  866. static void blas_memory_init(){
  867. #if defined(SMP)
  868. # if defined(OS_WINDOWS)
  869. local_storage_key = TlsAlloc();
  870. # else
  871. pthread_key_create(&local_storage_key, blas_memory_cleanup);
  872. # endif /* defined(OS_WINDOWS) */
  873. #endif /* defined(SMP) */
  874. }
  875. void *blas_memory_alloc(int procpos){
  876. int position;
  877. void *map_address;
  878. void *(*memoryalloc[])(void *address) = {
  879. #ifdef ALLOC_DEVICEDRIVER
  880. alloc_devicedirver,
  881. #endif
  882. /* Hugetlb implicitly assumes ALLOC_SHM */
  883. #ifdef ALLOC_SHM
  884. alloc_shm,
  885. #endif
  886. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  887. alloc_hugetlb,
  888. #endif
  889. #ifdef ALLOC_MMAP
  890. alloc_mmap,
  891. #endif
  892. #ifdef ALLOC_QALLOC
  893. alloc_qalloc,
  894. #endif
  895. #ifdef ALLOC_WINDOWS
  896. alloc_windows,
  897. #endif
  898. #ifdef ALLOC_MALLOC
  899. alloc_malloc,
  900. #endif
  901. NULL,
  902. };
  903. void *(**func)(void *address);
  904. struct alloc_t * alloc_info;
  905. struct alloc_t ** alloc_table;
  906. #if defined(SMP) && !defined(USE_OPENMP)
  907. int mi;
  908. LOCK_COMMAND(&alloc_lock);
  909. mi=memory_initialized;
  910. UNLOCK_COMMAND(&alloc_lock);
  911. if (!LIKELY_ONE(mi)) {
  912. #else
  913. if (!LIKELY_ONE(memory_initialized)) {
  914. #endif
  915. #if defined(SMP) && !defined(USE_OPENMP)
  916. /* Only allow a single thread to initialize memory system */
  917. LOCK_COMMAND(&alloc_lock);
  918. if (!memory_initialized) {
  919. #endif
  920. blas_memory_init();
  921. #ifdef DYNAMIC_ARCH
  922. gotoblas_dynamic_init();
  923. #endif
  924. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  925. gotoblas_affinity_init();
  926. #endif
  927. #ifdef SMP
  928. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  929. #endif
  930. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  931. #ifndef DYNAMIC_ARCH
  932. blas_set_parameter();
  933. #endif
  934. #endif
  935. memory_initialized = 1;
  936. #if defined(SMP) && !defined(USE_OPENMP)
  937. }
  938. UNLOCK_COMMAND(&alloc_lock);
  939. #endif
  940. }
  941. #ifdef DEBUG
  942. printf("Alloc Start ...\n");
  943. #endif
  944. position = 0;
  945. alloc_table = get_memory_table();
  946. do {
  947. if (!alloc_table[position] || !alloc_table[position]->used) goto allocation;
  948. position ++;
  949. } while (position < NUM_BUFFERS);
  950. goto error;
  951. allocation :
  952. #ifdef DEBUG
  953. printf(" Position -> %d\n", position);
  954. #endif
  955. alloc_info = alloc_table[position];
  956. if (!alloc_info) {
  957. do {
  958. #ifdef DEBUG
  959. printf("Allocation Start : %lx\n", base_address);
  960. #endif
  961. map_address = (void *)-1;
  962. func = &memoryalloc[0];
  963. while ((*func != NULL) && (map_address == (void *) -1)) {
  964. map_address = (*func)((void *)base_address);
  965. #ifdef ALLOC_DEVICEDRIVER
  966. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  967. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation failed.\n");
  968. }
  969. #endif
  970. #ifdef ALLOC_HUGETLBFILE
  971. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  972. #ifndef OS_WINDOWS
  973. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation failed.\n");
  974. #endif
  975. }
  976. #endif
  977. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  978. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  979. #endif
  980. func ++;
  981. }
  982. #ifdef DEBUG
  983. printf(" Success -> %08lx\n", map_address);
  984. #endif
  985. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  986. if (base_address) base_address += allocation_block_size + FIXED_PAGESIZE;
  987. } while ((BLASLONG)map_address == -1);
  988. alloc_table[position] = alloc_info = map_address;
  989. #ifdef DEBUG
  990. printf(" Mapping Succeeded. %p(%d)\n", (void *)alloc_info, position);
  991. #endif
  992. }
  993. #ifdef DEBUG
  994. printf("Mapped : %p %3d\n\n", (void *)alloc_info, position);
  995. #endif
  996. alloc_info->used = 1;
  997. return (void *)(((char *)alloc_info) + sizeof(struct alloc_t));
  998. error:
  999. printf("OpenBLAS : Program will terminate because you tried to allocate too many TLS memory regions.\n");
  1000. printf("This library was built to support a maximum of %d threads - either rebuild OpenBLAS\n", NUM_BUFFERS);
  1001. printf("with a larger NUM_THREADS value or set the environment variable OPENBLAS_NUM_THREADS to\n");
  1002. printf("a sufficiently small number. This error typically occurs when the software that relies on\n");
  1003. printf("OpenBLAS calls BLAS functions from many threads in parallel, or when your computer has more\n");
  1004. printf("cpu cores than what OpenBLAS was configured to handle.\n");
  1005. return NULL;
  1006. }
  1007. void blas_memory_free(void *buffer){
  1008. #ifdef DEBUG
  1009. int position;
  1010. struct alloc_t ** alloc_table;
  1011. #endif
  1012. /* Since we passed an offset pointer to the caller, get back to the actual allocation */
  1013. struct alloc_t *alloc_info = (void *)(((char *)buffer) - sizeof(struct alloc_t));
  1014. #ifdef DEBUG
  1015. printf("Unmapped Start : %p ...\n", alloc_info);
  1016. #endif
  1017. alloc_info->used = 0;
  1018. #ifdef DEBUG
  1019. printf("Unmap Succeeded.\n\n");
  1020. #endif
  1021. return;
  1022. #ifdef DEBUG
  1023. alloc_table = get_memory_table();
  1024. for (position = 0; position < NUM_BUFFERS; position++){
  1025. if (alloc_table[position]) {
  1026. printf("%4ld %p : %d\n", position, alloc_table[position], alloc_table[position]->used);
  1027. }
  1028. }
  1029. #endif
  1030. return;
  1031. }
  1032. void *blas_memory_alloc_nolock(int unused) {
  1033. void *map_address;
  1034. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1035. return map_address;
  1036. }
  1037. void blas_memory_free_nolock(void * map_address) {
  1038. free(map_address);
  1039. }
  1040. #ifdef SMP
  1041. void blas_thread_memory_cleanup(void) {
  1042. blas_memory_cleanup((void*)get_memory_table());
  1043. }
  1044. #endif
  1045. void blas_shutdown(void){
  1046. #ifdef SMP
  1047. BLASFUNC(blas_thread_shutdown)();
  1048. #endif
  1049. #ifdef SMP
  1050. /* Only cleanupIf we were built for threading and TLS was initialized */
  1051. if (local_storage_key)
  1052. #endif
  1053. blas_thread_memory_cleanup();
  1054. #ifdef SEEK_ADDRESS
  1055. base_address = 0UL;
  1056. #else
  1057. base_address = BASE_ADDRESS;
  1058. #endif
  1059. return;
  1060. }
  1061. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1062. #ifdef SMP
  1063. #if defined(USE_PTHREAD_LOCK)
  1064. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  1065. #elif defined(USE_PTHREAD_SPINLOCK)
  1066. static pthread_spinlock_t init_lock = 0;
  1067. #else
  1068. static BLASULONG init_lock = 0UL;
  1069. #endif
  1070. #endif
  1071. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  1072. void *sa, void *sb, BLASLONG pos) {
  1073. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  1074. size_t size;
  1075. BLASULONG buffer;
  1076. size = allocation_block_size - PAGESIZE;
  1077. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1078. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1079. if (hot_alloc != 2) {
  1080. #endif
  1081. #ifdef SMP
  1082. LOCK_COMMAND(&init_lock);
  1083. #endif
  1084. while (size > 0) {
  1085. *(int *)buffer = size;
  1086. buffer += PAGESIZE;
  1087. size -= PAGESIZE;
  1088. }
  1089. #ifdef SMP
  1090. UNLOCK_COMMAND(&init_lock);
  1091. #endif
  1092. size = MIN((allocation_block_size - PAGESIZE), L2_SIZE);
  1093. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1094. while (size > 0) {
  1095. *(int *)buffer = size;
  1096. buffer += 64;
  1097. size -= 64;
  1098. }
  1099. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1100. }
  1101. #endif
  1102. #endif
  1103. }
  1104. #ifdef SMP
  1105. static void _init_thread_memory(void *buffer) {
  1106. blas_queue_t queue[MAX_CPU_NUMBER];
  1107. int num_cpu;
  1108. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  1109. blas_queue_init(&queue[num_cpu]);
  1110. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  1111. queue[num_cpu].routine = &_touch_memory;
  1112. queue[num_cpu].args = NULL;
  1113. queue[num_cpu].next = &queue[num_cpu + 1];
  1114. }
  1115. queue[num_cpu - 1].next = NULL;
  1116. queue[0].sa = buffer;
  1117. exec_blas(num_cpu, queue);
  1118. }
  1119. #endif
  1120. static void gotoblas_memory_init(void) {
  1121. void *buffer;
  1122. hot_alloc = 1;
  1123. buffer = (void *)blas_memory_alloc(0);
  1124. #ifdef SMP
  1125. if (blas_cpu_number == 0) blas_get_cpu_number();
  1126. #ifdef SMP_SERVER
  1127. if (blas_server_avail == 0) blas_thread_init();
  1128. #endif
  1129. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  1130. #else
  1131. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  1132. #endif
  1133. blas_memory_free(buffer);
  1134. }
  1135. #endif
  1136. /* Initialization for all function; this function should be called before main */
  1137. static int gotoblas_initialized = 0;
  1138. extern void openblas_read_env();
  1139. void CONSTRUCTOR gotoblas_init(void) {
  1140. if (gotoblas_initialized) return;
  1141. #ifdef SMP
  1142. openblas_fork_handler();
  1143. #endif
  1144. openblas_read_env();
  1145. #ifdef PROFILE
  1146. moncontrol (0);
  1147. #endif
  1148. #ifdef DYNAMIC_ARCH
  1149. gotoblas_dynamic_init();
  1150. #endif
  1151. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1152. gotoblas_affinity_init();
  1153. #endif
  1154. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1155. gotoblas_memory_init();
  1156. #endif
  1157. //#if defined(OS_LINUX)
  1158. #if 0
  1159. struct rlimit curlimit;
  1160. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  1161. {
  1162. if ( curlimit.rlim_cur != curlimit.rlim_max )
  1163. {
  1164. curlimit.rlim_cur = curlimit.rlim_max;
  1165. setrlimit(RLIMIT_STACK, &curlimit);
  1166. }
  1167. }
  1168. #endif
  1169. #ifdef SMP
  1170. if (blas_cpu_number == 0) blas_get_cpu_number();
  1171. #ifdef SMP_SERVER
  1172. if (blas_server_avail == 0) blas_thread_init();
  1173. #endif
  1174. #endif
  1175. #ifdef FUNCTION_PROFILE
  1176. gotoblas_profile_init();
  1177. #endif
  1178. gotoblas_initialized = 1;
  1179. #ifdef PROFILE
  1180. moncontrol (1);
  1181. #endif
  1182. }
  1183. void DESTRUCTOR gotoblas_quit(void) {
  1184. if (gotoblas_initialized == 0) return;
  1185. blas_shutdown();
  1186. #if defined(SMP)
  1187. #if defined(OS_WINDOWS)
  1188. TlsFree(local_storage_key);
  1189. #else
  1190. pthread_key_delete(local_storage_key);
  1191. #endif
  1192. #endif
  1193. #ifdef PROFILE
  1194. moncontrol (0);
  1195. #endif
  1196. #ifdef FUNCTION_PROFILE
  1197. gotoblas_profile_quit();
  1198. #endif
  1199. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1200. gotoblas_affinity_quit();
  1201. #endif
  1202. #ifdef DYNAMIC_ARCH
  1203. gotoblas_dynamic_quit();
  1204. #endif
  1205. gotoblas_initialized = 0;
  1206. #ifdef PROFILE
  1207. moncontrol (1);
  1208. #endif
  1209. }
  1210. #if defined(_MSC_VER) && !defined(__clang__)
  1211. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  1212. {
  1213. switch (ul_reason_for_call)
  1214. {
  1215. case DLL_PROCESS_ATTACH:
  1216. gotoblas_init();
  1217. break;
  1218. case DLL_THREAD_ATTACH:
  1219. break;
  1220. case DLL_THREAD_DETACH:
  1221. #if defined(SMP)
  1222. blas_thread_memory_cleanup();
  1223. #endif
  1224. break;
  1225. case DLL_PROCESS_DETACH:
  1226. gotoblas_quit();
  1227. break;
  1228. default:
  1229. break;
  1230. }
  1231. return TRUE;
  1232. }
  1233. /*
  1234. This is to allow static linking.
  1235. Code adapted from Google performance tools:
  1236. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  1237. Reference:
  1238. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  1239. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  1240. */
  1241. static int on_process_term(void)
  1242. {
  1243. gotoblas_quit();
  1244. return 0;
  1245. }
  1246. #ifdef _WIN64
  1247. #pragma comment(linker, "/INCLUDE:_tls_used")
  1248. #else
  1249. #pragma comment(linker, "/INCLUDE:__tls_used")
  1250. #endif
  1251. #ifdef _WIN64
  1252. #pragma const_seg(".CRT$XLB")
  1253. #else
  1254. #pragma data_seg(".CRT$XLB")
  1255. #endif
  1256. #ifdef _WIN64
  1257. static const PIMAGE_TLS_CALLBACK dll_callback(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  1258. #pragma const_seg()
  1259. #else
  1260. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  1261. #pragma data_seg()
  1262. #endif
  1263. #ifdef _WIN64
  1264. #pragma const_seg(".CRT$XTU")
  1265. #else
  1266. #pragma data_seg(".CRT$XTU")
  1267. #endif
  1268. #ifdef _WIN64
  1269. static const int(*p_process_term)(void) = on_process_term;
  1270. #pragma const_seg()
  1271. #else
  1272. static int(*p_process_term)(void) = on_process_term;
  1273. #pragma data_seg()
  1274. #endif
  1275. #endif
  1276. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  1277. /* Don't call me; this is just work around for PGI / Sun bug */
  1278. void gotoblas_dummy_for_PGI(void) {
  1279. gotoblas_init();
  1280. gotoblas_quit();
  1281. #if __PGIC__ < 19
  1282. #if 0
  1283. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  1284. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  1285. #else
  1286. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  1287. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  1288. #endif
  1289. #endif
  1290. }
  1291. #endif
  1292. #else
  1293. /* USE_TLS / COMPILE_TLS not set */
  1294. #include <errno.h>
  1295. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  1296. #define ALLOC_WINDOWS
  1297. #ifndef MEM_LARGE_PAGES
  1298. #define MEM_LARGE_PAGES 0x20000000
  1299. #endif
  1300. #elif !defined(OS_EMBEDDED)
  1301. #define ALLOC_MMAP
  1302. #define ALLOC_MALLOC
  1303. #else
  1304. #define ALLOC_MALLOC
  1305. inline int puts(const char *str) { return 0; }
  1306. inline int printf(const char *format, ...) { return 0; }
  1307. inline char *getenv(const char *name) { return ""; }
  1308. inline int atoi(const char *str) { return 0; }
  1309. #endif
  1310. #include <stdlib.h>
  1311. #include <stdio.h>
  1312. #include <fcntl.h>
  1313. #if (!defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)) && !defined(OS_EMBEDDED)
  1314. #include <sys/mman.h>
  1315. #ifndef NO_SYSV_IPC
  1316. #include <sys/shm.h>
  1317. #endif
  1318. #include <sys/ipc.h>
  1319. #endif
  1320. #include <sys/types.h>
  1321. #ifdef OS_LINUX
  1322. #include <sys/sysinfo.h>
  1323. #include <sched.h>
  1324. #include <errno.h>
  1325. #include <sys/syscall.h>
  1326. #include <sys/time.h>
  1327. #include <sys/resource.h>
  1328. #endif
  1329. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
  1330. #include <sys/sysctl.h>
  1331. #include <sys/resource.h>
  1332. #endif
  1333. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  1334. #include <conio.h>
  1335. #undef printf
  1336. #define printf _cprintf
  1337. #endif
  1338. #ifdef OS_LINUX
  1339. #ifndef MPOL_PREFERRED
  1340. #define MPOL_PREFERRED 1
  1341. #endif
  1342. #endif
  1343. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  1344. #define NO_WARMUP
  1345. #endif
  1346. #ifndef SHM_HUGETLB
  1347. #define SHM_HUGETLB 04000
  1348. #endif
  1349. #ifndef FIXED_PAGESIZE
  1350. #define FIXED_PAGESIZE 4096
  1351. #endif
  1352. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  1353. #if defined(_MSC_VER) && !defined(__clang__)
  1354. #define CONSTRUCTOR __cdecl
  1355. #define DESTRUCTOR __cdecl
  1356. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  1357. #define CONSTRUCTOR __attribute__ ((constructor))
  1358. #define DESTRUCTOR __attribute__ ((destructor))
  1359. #elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
  1360. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  1361. #define DESTRUCTOR __attribute__ ((destructor(101)))
  1362. #else
  1363. #define CONSTRUCTOR __attribute__ ((constructor))
  1364. #define DESTRUCTOR __attribute__ ((destructor))
  1365. #endif
  1366. #ifdef DYNAMIC_ARCH
  1367. gotoblas_t *gotoblas = NULL;
  1368. #endif
  1369. extern void openblas_warning(int verbose, const char * msg);
  1370. #ifndef SMP
  1371. #define blas_cpu_number 1
  1372. #define blas_num_threads 1
  1373. /* Dummy Function */
  1374. int goto_get_num_procs (void) { return 1;};
  1375. void goto_set_num_threads(int num_threads) {};
  1376. #else
  1377. #if defined(OS_LINUX) || defined(OS_SUNOS)
  1378. #ifndef NO_AFFINITY
  1379. int get_num_procs(void);
  1380. #else
  1381. int get_num_procs(void) {
  1382. static int nums = 0;
  1383. #if defined(__GLIBC_PREREQ)
  1384. cpu_set_t cpuset,*cpusetp;
  1385. size_t size;
  1386. int ret;
  1387. #if !__GLIBC_PREREQ(2, 7)
  1388. int i;
  1389. #if !__GLIBC_PREREQ(2, 6)
  1390. int n;
  1391. #endif
  1392. #endif
  1393. #endif
  1394. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1395. #if !defined(OS_LINUX)
  1396. return nums;
  1397. #endif
  1398. #if !defined(__GLIBC_PREREQ)
  1399. return nums;
  1400. #else
  1401. #if !__GLIBC_PREREQ(2, 3)
  1402. return nums;
  1403. #endif
  1404. #if !__GLIBC_PREREQ(2, 7)
  1405. ret = sched_getaffinity(0,sizeof(cpuset), &cpuset);
  1406. if (ret!=0) return nums;
  1407. n=0;
  1408. #if !__GLIBC_PREREQ(2, 6)
  1409. for (i=0;i<nums;i++)
  1410. if (CPU_ISSET(i,&cpuset)) n++;
  1411. nums=n;
  1412. #else
  1413. nums = CPU_COUNT(sizeof(cpuset),&cpuset);
  1414. #endif
  1415. return nums;
  1416. #else
  1417. if (nums >= CPU_SETSIZE) {
  1418. cpusetp = CPU_ALLOC(nums);
  1419. if (cpusetp == NULL) {
  1420. return nums;
  1421. }
  1422. size = CPU_ALLOC_SIZE(nums);
  1423. ret = sched_getaffinity(0,size,cpusetp);
  1424. if (ret!=0) {
  1425. CPU_FREE(cpusetp);
  1426. return nums;
  1427. }
  1428. ret = CPU_COUNT_S(size,cpusetp);
  1429. if (ret > 0 && ret < nums) nums = ret;
  1430. CPU_FREE(cpusetp);
  1431. return nums;
  1432. } else {
  1433. ret = sched_getaffinity(0,sizeof(cpuset),&cpuset);
  1434. if (ret!=0) {
  1435. return nums;
  1436. }
  1437. ret = CPU_COUNT(&cpuset);
  1438. if (ret > 0 && ret < nums) nums = ret;
  1439. return nums;
  1440. }
  1441. #endif
  1442. #endif
  1443. }
  1444. #endif
  1445. #endif
  1446. #ifdef OS_ANDROID
  1447. int get_num_procs(void) {
  1448. static int nums = 0;
  1449. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1450. return nums;
  1451. }
  1452. #endif
  1453. #ifdef OS_HAIKU
  1454. int get_num_procs(void) {
  1455. static int nums = 0;
  1456. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1457. return nums;
  1458. }
  1459. #endif
  1460. #ifdef OS_AIX
  1461. int get_num_procs(void) {
  1462. static int nums = 0;
  1463. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1464. return nums;
  1465. }
  1466. #endif
  1467. #ifdef OS_WINDOWS
  1468. int get_num_procs(void) {
  1469. static int nums = 0;
  1470. if (nums == 0) {
  1471. SYSTEM_INFO sysinfo;
  1472. GetSystemInfo(&sysinfo);
  1473. nums = sysinfo.dwNumberOfProcessors;
  1474. }
  1475. return nums;
  1476. }
  1477. #endif
  1478. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY)
  1479. int get_num_procs(void) {
  1480. static int nums = 0;
  1481. int m[2];
  1482. size_t len;
  1483. if (nums == 0) {
  1484. m[0] = CTL_HW;
  1485. m[1] = HW_NCPU;
  1486. len = sizeof(int);
  1487. sysctl(m, 2, &nums, &len, NULL, 0);
  1488. }
  1489. return nums;
  1490. }
  1491. #endif
  1492. #if defined(OS_DARWIN)
  1493. int get_num_procs(void) {
  1494. static int nums = 0;
  1495. size_t len;
  1496. if (nums == 0){
  1497. len = sizeof(int);
  1498. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  1499. }
  1500. return nums;
  1501. }
  1502. /*
  1503. void set_stack_limit(int limitMB){
  1504. int result=0;
  1505. struct rlimit rl;
  1506. rlim_t StackSize;
  1507. StackSize=limitMB*1024*1024;
  1508. result=getrlimit(RLIMIT_STACK, &rl);
  1509. if(result==0){
  1510. if(rl.rlim_cur < StackSize){
  1511. rl.rlim_cur=StackSize;
  1512. result=setrlimit(RLIMIT_STACK, &rl);
  1513. if(result !=0){
  1514. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  1515. }
  1516. }
  1517. }
  1518. }
  1519. */
  1520. #endif
  1521. /*
  1522. OpenBLAS uses the numbers of CPU cores in multithreading.
  1523. It can be set by openblas_set_num_threads(int num_threads);
  1524. */
  1525. int blas_cpu_number = 0;
  1526. /*
  1527. The numbers of threads in the thread pool.
  1528. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  1529. */
  1530. int blas_num_threads = 0;
  1531. int goto_get_num_procs (void) {
  1532. return blas_cpu_number;
  1533. }
  1534. void openblas_fork_handler()
  1535. {
  1536. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  1537. // built with "make USE_OPENMP=0".
  1538. // Hanging can still happen when OpenBLAS is built against the libgomp
  1539. // implementation of OpenMP. The problem is tracked at:
  1540. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  1541. // In the mean time build with USE_OPENMP=0 or link against another
  1542. // implementation of OpenMP.
  1543. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  1544. int err;
  1545. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
  1546. if(err != 0)
  1547. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  1548. #endif
  1549. }
  1550. extern int openblas_num_threads_env();
  1551. extern int openblas_goto_num_threads_env();
  1552. extern int openblas_omp_num_threads_env();
  1553. int blas_get_cpu_number(void){
  1554. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_HAIKU)
  1555. int max_num;
  1556. #endif
  1557. int blas_goto_num = 0;
  1558. int blas_omp_num = 0;
  1559. if (blas_num_threads) return blas_num_threads;
  1560. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_HAIKU)
  1561. max_num = get_num_procs();
  1562. #endif
  1563. // blas_goto_num = 0;
  1564. #ifndef USE_OPENMP
  1565. blas_goto_num=openblas_num_threads_env();
  1566. if (blas_goto_num < 0) blas_goto_num = 0;
  1567. if (blas_goto_num == 0) {
  1568. blas_goto_num=openblas_goto_num_threads_env();
  1569. if (blas_goto_num < 0) blas_goto_num = 0;
  1570. }
  1571. #endif
  1572. // blas_omp_num = 0;
  1573. blas_omp_num=openblas_omp_num_threads_env();
  1574. if (blas_omp_num < 0) blas_omp_num = 0;
  1575. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  1576. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  1577. else blas_num_threads = MAX_CPU_NUMBER;
  1578. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID) || defined(OS_HAIKU)
  1579. if (blas_num_threads > max_num) blas_num_threads = max_num;
  1580. #endif
  1581. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  1582. #ifdef DEBUG
  1583. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  1584. #endif
  1585. blas_cpu_number = blas_num_threads;
  1586. return blas_num_threads;
  1587. }
  1588. #endif
  1589. int openblas_get_num_procs(void) {
  1590. #ifndef SMP
  1591. return 1;
  1592. #else
  1593. return get_num_procs();
  1594. #endif
  1595. }
  1596. int openblas_get_num_threads(void) {
  1597. #ifndef SMP
  1598. return 1;
  1599. #else
  1600. // init blas_cpu_number if needed
  1601. blas_get_cpu_number();
  1602. return blas_cpu_number;
  1603. #endif
  1604. }
  1605. struct release_t {
  1606. void *address;
  1607. void (*func)(struct release_t *);
  1608. long attr;
  1609. };
  1610. int hugetlb_allocated = 0;
  1611. static struct release_t release_info[NUM_BUFFERS];
  1612. static struct release_t *new_release_info;
  1613. static int release_pos = 0;
  1614. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1615. static int hot_alloc = 0;
  1616. #endif
  1617. /* Global lock for memory allocation */
  1618. #if defined(USE_PTHREAD_LOCK)
  1619. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  1620. #elif defined(USE_PTHREAD_SPINLOCK)
  1621. static pthread_spinlock_t alloc_lock = 0;
  1622. #else
  1623. static BLASULONG alloc_lock = 0UL;
  1624. #endif
  1625. #ifdef ALLOC_MMAP
  1626. static void alloc_mmap_free(struct release_t *release){
  1627. if (!release->address) return;
  1628. if (munmap(release -> address, BUFFER_SIZE)) {
  1629. int errsv=errno;
  1630. perror("OpenBLAS : munmap failed:");
  1631. printf("error code=%d,\trelease->address=%p\n",errsv,release->address);
  1632. }
  1633. }
  1634. #ifdef NO_WARMUP
  1635. static void *alloc_mmap(void *address){
  1636. void *map_address;
  1637. if (address){
  1638. map_address = mmap(address,
  1639. BUFFER_SIZE,
  1640. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1641. } else {
  1642. map_address = mmap(address,
  1643. BUFFER_SIZE,
  1644. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1645. }
  1646. if (map_address != (void *)-1) {
  1647. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1648. LOCK_COMMAND(&alloc_lock);
  1649. #endif
  1650. if (likely(release_pos < NUM_BUFFERS)) {
  1651. release_info[release_pos].address = map_address;
  1652. release_info[release_pos].func = alloc_mmap_free;
  1653. } else {
  1654. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1655. new_release_info[release_pos-NUM_BUFFERS].func = alloc_mmap_free;
  1656. }
  1657. release_pos ++;
  1658. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1659. UNLOCK_COMMAND(&alloc_lock);
  1660. #endif
  1661. } else {
  1662. #ifdef DEBUG
  1663. int errsv=errno;
  1664. perror("OpenBLAS : mmap failed:");
  1665. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1666. #endif
  1667. }
  1668. #ifdef OS_LINUX
  1669. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1670. #endif
  1671. return map_address;
  1672. }
  1673. #else
  1674. #define BENCH_ITERATION 4
  1675. #define SCALING 2
  1676. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  1677. BLASULONG original, *p;
  1678. BLASULONG start, stop, min;
  1679. int iter, i, count;
  1680. min = (BLASULONG)-1;
  1681. original = *(BLASULONG *)(address + size - PAGESIZE);
  1682. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  1683. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  1684. p = (BLASULONG *)address;
  1685. count = size / PAGESIZE;
  1686. start = rpcc();
  1687. for (i = 0; i < count; i ++) {
  1688. p = (BLASULONG *)(*p);
  1689. }
  1690. stop = rpcc();
  1691. if (min > stop - start) min = stop - start;
  1692. }
  1693. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  1694. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  1695. return min;
  1696. }
  1697. static void *alloc_mmap(void *address){
  1698. void *map_address, *best_address;
  1699. BLASULONG best, start, current;
  1700. BLASULONG allocsize;
  1701. if (address){
  1702. /* Just give up use advanced operation */
  1703. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1704. #ifdef OS_LINUX
  1705. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1706. #endif
  1707. } else {
  1708. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1709. if (hot_alloc == 0) {
  1710. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1711. #ifdef OS_LINUX
  1712. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1713. #endif
  1714. } else {
  1715. #endif
  1716. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  1717. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1718. if (map_address != (void *)-1) {
  1719. #ifdef OS_LINUX
  1720. #ifdef DEBUG
  1721. int ret=0;
  1722. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1723. if(ret==-1){
  1724. int errsv=errno;
  1725. perror("OpenBLAS alloc_mmap:");
  1726. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1727. }
  1728. #else
  1729. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1730. #endif
  1731. #endif
  1732. #ifdef BUILD_DOUBLE
  1733. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  1734. #elif defined(BUILD_COMPLEX16)
  1735. allocsize = ZGEMM_P * ZGEMM_Q * sizeof(double);
  1736. #elif defined(BUILD_COMPLEX)
  1737. allocsize = CGEMM_P * CGEMM_Q * sizeof(double);
  1738. #else
  1739. allocsize = SGEMM_P * SGEMM_Q * sizeof(double);
  1740. #endif
  1741. start = (BLASULONG)map_address;
  1742. current = (SCALING - 1) * BUFFER_SIZE;
  1743. while(current > 0) {
  1744. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  1745. start += PAGESIZE;
  1746. current -= PAGESIZE;
  1747. }
  1748. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  1749. start = (BLASULONG)map_address;
  1750. best = (BLASULONG)-1;
  1751. best_address = map_address;
  1752. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  1753. current = run_bench(start, allocsize);
  1754. if (best > current) {
  1755. best = current;
  1756. best_address = (void *)start;
  1757. }
  1758. start += PAGESIZE;
  1759. }
  1760. if ((BLASULONG)best_address > (BLASULONG)map_address)
  1761. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  1762. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  1763. map_address = best_address;
  1764. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1765. hot_alloc = 2;
  1766. #endif
  1767. }
  1768. }
  1769. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1770. }
  1771. #endif
  1772. if (map_address != (void *)-1) {
  1773. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1774. LOCK_COMMAND(&alloc_lock);
  1775. #endif
  1776. if (likely(release_pos < NUM_BUFFERS)) {
  1777. release_info[release_pos].address = map_address;
  1778. release_info[release_pos].func = alloc_mmap_free;
  1779. } else {
  1780. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1781. new_release_info[release_pos-NUM_BUFFERS].func = alloc_mmap_free;
  1782. }
  1783. release_pos ++;
  1784. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1785. UNLOCK_COMMAND(&alloc_lock);
  1786. #endif
  1787. }
  1788. return map_address;
  1789. }
  1790. #endif
  1791. #endif
  1792. #ifdef ALLOC_MALLOC
  1793. static void alloc_malloc_free(struct release_t *release){
  1794. free(release -> address);
  1795. }
  1796. static void *alloc_malloc(void *address){
  1797. void *map_address;
  1798. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1799. if (map_address == (void *)NULL) map_address = (void *)-1;
  1800. if (map_address != (void *)-1) {
  1801. if (likely(release_pos < NUM_BUFFERS)) {
  1802. release_info[release_pos].address = map_address;
  1803. release_info[release_pos].func = alloc_malloc_free;
  1804. } else {
  1805. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1806. new_release_info[release_pos-NUM_BUFFERS].func = alloc_malloc_free;
  1807. }
  1808. release_pos ++;
  1809. }
  1810. return map_address;
  1811. }
  1812. #endif
  1813. #ifdef ALLOC_QALLOC
  1814. void *qalloc(int flags, size_t bytes);
  1815. void *qfree (void *address);
  1816. #define QNONCACHE 0x1
  1817. #define QCOMMS 0x2
  1818. #define QFAST 0x4
  1819. static void alloc_qalloc_free(struct release_t *release){
  1820. qfree(release -> address);
  1821. }
  1822. static void *alloc_qalloc(void *address){
  1823. void *map_address;
  1824. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  1825. if (map_address == (void *)NULL) map_address = (void *)-1;
  1826. if (map_address != (void *)-1) {
  1827. if (likely(release_pos < NUM_BUFFERS)) {
  1828. release_info[release_pos].address = map_address;
  1829. release_info[release_pos].func = alloc_qalloc_free;
  1830. } else {
  1831. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1832. new_release_info[release_pos-NUM_BUFFERS].func = alloc_qalloc_free;
  1833. }
  1834. release_pos ++;
  1835. }
  1836. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  1837. }
  1838. #endif
  1839. #ifdef ALLOC_WINDOWS
  1840. static void alloc_windows_free(struct release_t *release){
  1841. VirtualFree(release -> address, 0, MEM_RELEASE);
  1842. }
  1843. static void *alloc_windows(void *address){
  1844. void *map_address;
  1845. map_address = VirtualAlloc(address,
  1846. BUFFER_SIZE,
  1847. MEM_RESERVE | MEM_COMMIT,
  1848. PAGE_READWRITE);
  1849. if (map_address == (void *)NULL) map_address = (void *)-1;
  1850. if (map_address != (void *)-1) {
  1851. if (likely(release_pos < NUM_BUFFERS)) {
  1852. release_info[release_pos].address = map_address;
  1853. release_info[release_pos].func = alloc_windows_free;
  1854. } else {
  1855. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1856. new_release_info[release_pos-NUM_BUFFERS].func = alloc_windows_free;
  1857. }
  1858. release_pos ++;
  1859. }
  1860. return map_address;
  1861. }
  1862. #endif
  1863. #ifdef ALLOC_DEVICEDRIVER
  1864. #ifndef DEVICEDRIVER_NAME
  1865. #define DEVICEDRIVER_NAME "/dev/mapper"
  1866. #endif
  1867. static void alloc_devicedirver_free(struct release_t *release){
  1868. if (munmap(release -> address, BUFFER_SIZE)) {
  1869. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  1870. }
  1871. if (close(release -> attr)) {
  1872. printf("OpenBLAS : Bugphysarea close failed.\n");
  1873. }
  1874. }
  1875. static void *alloc_devicedirver(void *address){
  1876. int fd;
  1877. void *map_address;
  1878. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  1879. return (void *)-1;
  1880. }
  1881. map_address = mmap(address, BUFFER_SIZE,
  1882. PROT_READ | PROT_WRITE,
  1883. MAP_FILE | MAP_SHARED,
  1884. fd, 0);
  1885. if (map_address != (void *)-1) {
  1886. if (likely(release_pos < NUM_BUFFERS)) {
  1887. release_info[release_pos].address = map_address;
  1888. release_info[release_pos].attr = fd;
  1889. release_info[release_pos].func = alloc_devicedirver_free;
  1890. } else {
  1891. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1892. new_release_info[release_pos-NUM_BUFFERS].attr = fd;
  1893. new_release_info[release_pos-NUM_BUFFERS].func = alloc_devicedirver_free;
  1894. }
  1895. release_pos ++;
  1896. }
  1897. return map_address;
  1898. }
  1899. #endif
  1900. #ifdef ALLOC_SHM
  1901. static void alloc_shm_free(struct release_t *release){
  1902. if (shmdt(release -> address)) {
  1903. printf("OpenBLAS : Shared memory unmap failed.\n");
  1904. }
  1905. }
  1906. static void *alloc_shm(void *address){
  1907. void *map_address;
  1908. int shmid;
  1909. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  1910. map_address = (void *)shmat(shmid, address, 0);
  1911. if (map_address != (void *)-1){
  1912. #ifdef OS_LINUX
  1913. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1914. #endif
  1915. shmctl(shmid, IPC_RMID, 0);
  1916. if (likely(release_pos < NUM_BUFFERS)) {
  1917. release_info[release_pos].address = map_address;
  1918. release_info[release_pos].attr = shmid;
  1919. release_info[release_pos].func = alloc_shm_free;
  1920. } else {
  1921. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1922. new_release_info[release_pos-NUM_BUFFERS].attr = shmid;
  1923. new_release_info[release_pos-NUM_BUFFERS].func = alloc_shm_free;
  1924. }
  1925. release_pos ++;
  1926. }
  1927. return map_address;
  1928. }
  1929. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  1930. static void alloc_hugetlb_free(struct release_t *release){
  1931. #if defined(OS_LINUX) || defined(OS_AIX)
  1932. if (shmdt(release -> address)) {
  1933. printf("OpenBLAS : Hugepage unmap failed.\n");
  1934. }
  1935. #endif
  1936. #ifdef __sun__
  1937. munmap(release -> address, BUFFER_SIZE);
  1938. #endif
  1939. #ifdef OS_WINDOWS
  1940. VirtualFree(release -> address, 0, MEM_LARGE_PAGES | MEM_RELEASE);
  1941. #endif
  1942. }
  1943. static void *alloc_hugetlb(void *address){
  1944. void *map_address = (void *)-1;
  1945. #if defined(OS_LINUX) || defined(OS_AIX)
  1946. int shmid;
  1947. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  1948. #ifdef OS_LINUX
  1949. SHM_HUGETLB |
  1950. #endif
  1951. #ifdef OS_AIX
  1952. SHM_LGPAGE | SHM_PIN |
  1953. #endif
  1954. IPC_CREAT | SHM_R | SHM_W);
  1955. if (shmid != -1) {
  1956. map_address = (void *)shmat(shmid, address, SHM_RND);
  1957. #ifdef OS_LINUX
  1958. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1959. #endif
  1960. if (map_address != (void *)-1){
  1961. shmctl(shmid, IPC_RMID, 0);
  1962. }
  1963. }
  1964. #endif
  1965. #ifdef __sun__
  1966. struct memcntl_mha mha;
  1967. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  1968. mha.mha_flags = 0;
  1969. mha.mha_pagesize = HUGE_PAGESIZE;
  1970. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  1971. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  1972. #endif
  1973. #ifdef OS_WINDOWS
  1974. HANDLE hToken;
  1975. TOKEN_PRIVILEGES tp;
  1976. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  1977. tp.PrivilegeCount = 1;
  1978. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1979. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  1980. CloseHandle(hToken);
  1981. return (void*)-1;
  1982. }
  1983. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  1984. CloseHandle(hToken);
  1985. return (void*)-1;
  1986. }
  1987. map_address = (void *)VirtualAlloc(address,
  1988. BUFFER_SIZE,
  1989. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  1990. PAGE_READWRITE);
  1991. tp.Privileges[0].Attributes = 0;
  1992. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  1993. if (map_address == (void *)NULL) map_address = (void *)-1;
  1994. #endif
  1995. if (map_address != (void *)-1){
  1996. if (likely(release_pos < NUM_BUFFERS)) {
  1997. release_info[release_pos].address = map_address;
  1998. release_info[release_pos].func = alloc_hugetlb_free;
  1999. } else {
  2000. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  2001. new_release_info[release_pos-NUM_BUFFERS].func = alloc_hugetlb_free;
  2002. }
  2003. release_pos ++;
  2004. }
  2005. return map_address;
  2006. }
  2007. #endif
  2008. #endif
  2009. #ifdef ALLOC_HUGETLBFILE
  2010. static int hugetlb_pid = 0;
  2011. static void alloc_hugetlbfile_free(struct release_t *release){
  2012. if (munmap(release -> address, BUFFER_SIZE)) {
  2013. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  2014. }
  2015. if (close(release -> attr)) {
  2016. printf("OpenBLAS : HugeTLBfs close failed.\n");
  2017. }
  2018. }
  2019. static void *alloc_hugetlbfile(void *address){
  2020. void *map_address = (void *)-1;
  2021. int fd;
  2022. char filename[64];
  2023. if (!hugetlb_pid) hugetlb_pid = getpid();
  2024. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  2025. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  2026. return (void *)-1;
  2027. }
  2028. unlink(filename);
  2029. map_address = mmap(address, BUFFER_SIZE,
  2030. PROT_READ | PROT_WRITE,
  2031. MAP_SHARED,
  2032. fd, 0);
  2033. if (map_address != (void *)-1) {
  2034. if (likely(release_pos < NUM_BUFFERS)) {
  2035. release_info[release_pos].address = map_address;
  2036. release_info[release_pos].attr = fd;
  2037. release_info[release_pos].func = alloc_hugetlbfile_free;
  2038. } else {
  2039. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  2040. new_release_info[release_pos-NUM_BUFFERS].attr = fd;
  2041. new_release_info[release_pos-NUM_BUFFERS].func = alloc_hugetlbfile_free;
  2042. }
  2043. release_pos ++;
  2044. }
  2045. return map_address;
  2046. }
  2047. #endif
  2048. #ifdef SEEK_ADDRESS
  2049. static BLASULONG base_address = 0UL;
  2050. #else
  2051. static BLASULONG base_address = BASE_ADDRESS;
  2052. #endif
  2053. static volatile struct {
  2054. BLASULONG lock;
  2055. void *addr;
  2056. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2057. int pos;
  2058. #endif
  2059. int used;
  2060. #ifndef __64BIT__
  2061. char dummy[48];
  2062. #else
  2063. char dummy[40];
  2064. #endif
  2065. } memory[NUM_BUFFERS];
  2066. struct newmemstruct
  2067. {
  2068. BLASULONG lock;
  2069. void *addr;
  2070. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2071. int pos;
  2072. #endif
  2073. int used;
  2074. #ifndef __64BIT__
  2075. char dummy[48];
  2076. #else
  2077. char dummy[40];
  2078. #endif
  2079. };
  2080. static volatile struct newmemstruct *newmemory;
  2081. static int memory_initialized = 0;
  2082. static int memory_overflowed = 0;
  2083. /* Memory allocation routine */
  2084. /* procpos ... indicates where it comes from */
  2085. /* 0 : Level 3 functions */
  2086. /* 1 : Level 2 functions */
  2087. /* 2 : Thread */
  2088. void *blas_memory_alloc(int procpos){
  2089. int i;
  2090. int position;
  2091. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2092. int mypos = 0;
  2093. #endif
  2094. void *map_address;
  2095. void *(*memoryalloc[])(void *address) = {
  2096. #ifdef ALLOC_DEVICEDRIVER
  2097. alloc_devicedirver,
  2098. #endif
  2099. /* Hugetlb implicitly assumes ALLOC_SHM */
  2100. #ifdef ALLOC_SHM
  2101. alloc_shm,
  2102. #endif
  2103. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  2104. alloc_hugetlb,
  2105. #endif
  2106. #ifdef ALLOC_MMAP
  2107. alloc_mmap,
  2108. #endif
  2109. #ifdef ALLOC_QALLOC
  2110. alloc_qalloc,
  2111. #endif
  2112. #ifdef ALLOC_WINDOWS
  2113. alloc_windows,
  2114. #endif
  2115. #ifdef ALLOC_MALLOC
  2116. alloc_malloc,
  2117. #endif
  2118. NULL,
  2119. };
  2120. void *(**func)(void *address);
  2121. #if defined(USE_OPENMP)
  2122. if (!memory_initialized) {
  2123. #endif
  2124. LOCK_COMMAND(&alloc_lock);
  2125. if (!memory_initialized) {
  2126. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2127. for (position = 0; position < NUM_BUFFERS; position ++){
  2128. memory[position].addr = (void *)0;
  2129. memory[position].pos = -1;
  2130. memory[position].used = 0;
  2131. memory[position].lock = 0;
  2132. }
  2133. #endif
  2134. #ifdef DYNAMIC_ARCH
  2135. gotoblas_dynamic_init();
  2136. #endif
  2137. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2138. gotoblas_affinity_init();
  2139. #endif
  2140. #ifdef SMP
  2141. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  2142. #endif
  2143. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  2144. #ifndef DYNAMIC_ARCH
  2145. blas_set_parameter();
  2146. #endif
  2147. #endif
  2148. memory_initialized = 1;
  2149. }
  2150. UNLOCK_COMMAND(&alloc_lock);
  2151. #if defined(USE_OPENMP)
  2152. }
  2153. #endif
  2154. #ifdef DEBUG
  2155. printf("Alloc Start ...\n");
  2156. #endif
  2157. /* #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2158. mypos = WhereAmI();
  2159. position = mypos;
  2160. while (position >= NUM_BUFFERS) position >>= 1;
  2161. do {
  2162. if (!memory[position].used && (memory[position].pos == mypos)) {
  2163. #if defined(SMP) && !defined(USE_OPENMP)
  2164. LOCK_COMMAND(&alloc_lock);
  2165. #else
  2166. blas_lock(&memory[position].lock);
  2167. #endif
  2168. if (!memory[position].used) goto allocation;
  2169. #if defined(SMP) && !defined(USE_OPENMP)
  2170. UNLOCK_COMMAND(&alloc_lock);
  2171. #else
  2172. blas_unlock(&memory[position].lock);
  2173. #endif
  2174. }
  2175. position ++;
  2176. } while (position < NUM_BUFFERS);
  2177. #endif */
  2178. position = 0;
  2179. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2180. LOCK_COMMAND(&alloc_lock);
  2181. #endif
  2182. do {
  2183. RMB;
  2184. #if defined(USE_OPENMP)
  2185. if (!memory[position].used) {
  2186. blas_lock(&memory[position].lock);
  2187. #endif
  2188. if (!memory[position].used) goto allocation;
  2189. #if defined(USE_OPENMP)
  2190. blas_unlock(&memory[position].lock);
  2191. }
  2192. #endif
  2193. position ++;
  2194. } while (position < NUM_BUFFERS);
  2195. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2196. UNLOCK_COMMAND(&alloc_lock);
  2197. #endif
  2198. if (memory_overflowed) {
  2199. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2200. LOCK_COMMAND(&alloc_lock);
  2201. #endif
  2202. do {
  2203. RMB;
  2204. #if defined(USE_OPENMP)
  2205. if (!newmemory[position-NUM_BUFFERS].used) {
  2206. blas_lock(&newmemory[position-NUM_BUFFERS].lock);
  2207. #endif
  2208. if (!newmemory[position-NUM_BUFFERS].used) goto allocation2;
  2209. #if defined(USE_OPENMP)
  2210. blas_unlock(&newmemory[position-NUM_BUFFERS].lock);
  2211. }
  2212. #endif
  2213. position ++;
  2214. } while (position < 512+NUM_BUFFERS);
  2215. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2216. UNLOCK_COMMAND(&alloc_lock);
  2217. #endif
  2218. }
  2219. goto error;
  2220. allocation :
  2221. #ifdef DEBUG
  2222. printf(" Position -> %d\n", position);
  2223. #endif
  2224. memory[position].used = 1;
  2225. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2226. UNLOCK_COMMAND(&alloc_lock);
  2227. #else
  2228. blas_unlock(&memory[position].lock);
  2229. #endif
  2230. if (!memory[position].addr) {
  2231. do {
  2232. #ifdef DEBUG
  2233. printf("Allocation Start : %lx\n", base_address);
  2234. #endif
  2235. map_address = (void *)-1;
  2236. func = &memoryalloc[0];
  2237. while ((func != NULL) && (map_address == (void *) -1)) {
  2238. map_address = (*func)((void *)base_address);
  2239. #ifdef ALLOC_DEVICEDRIVER
  2240. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  2241. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation was failed.\n");
  2242. }
  2243. #endif
  2244. #ifdef ALLOC_HUGETLBFILE
  2245. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  2246. #ifndef OS_WINDOWS
  2247. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  2248. #endif
  2249. }
  2250. #endif
  2251. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  2252. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  2253. #endif
  2254. func ++;
  2255. }
  2256. #ifdef DEBUG
  2257. printf(" Success -> %08lx\n", map_address);
  2258. #endif
  2259. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  2260. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  2261. } while ((BLASLONG)map_address == -1);
  2262. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2263. LOCK_COMMAND(&alloc_lock);
  2264. #endif
  2265. memory[position].addr = map_address;
  2266. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2267. UNLOCK_COMMAND(&alloc_lock);
  2268. #endif
  2269. #ifdef DEBUG
  2270. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  2271. #endif
  2272. }
  2273. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2274. if (memory[position].pos == -1) memory[position].pos = mypos;
  2275. #endif
  2276. #ifdef DYNAMIC_ARCH
  2277. if (memory_initialized == 1) {
  2278. LOCK_COMMAND(&alloc_lock);
  2279. if (memory_initialized == 1) {
  2280. if (!gotoblas) gotoblas_dynamic_init();
  2281. memory_initialized = 2;
  2282. }
  2283. UNLOCK_COMMAND(&alloc_lock);
  2284. }
  2285. #endif
  2286. #ifdef DEBUG
  2287. printf("Mapped : %p %3d\n\n",
  2288. (void *)memory[position].addr, position);
  2289. #endif
  2290. return (void *)memory[position].addr;
  2291. error:
  2292. if (memory_overflowed) goto terminate;
  2293. fprintf(stderr,"OpenBLAS warning: precompiled NUM_THREADS exceeded, adding auxiliary array for thread metadata.\n");
  2294. memory_overflowed=1;
  2295. new_release_info = (struct release_t*) malloc(512*sizeof(struct release_t));
  2296. newmemory = (struct newmemstruct*) malloc(512*sizeof(struct newmemstruct));
  2297. for (i = 0; i < 512; i++) {
  2298. newmemory[i].addr = (void *)0;
  2299. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2300. newmemory[i].pos = -1;
  2301. #endif
  2302. newmemory[i].used = 0;
  2303. newmemory[i].lock = 0;
  2304. }
  2305. newmemory[position-NUM_BUFFERS].used = 1;
  2306. allocation2:
  2307. newmemory[position-NUM_BUFFERS].used = 1;
  2308. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2309. UNLOCK_COMMAND(&alloc_lock);
  2310. #else
  2311. blas_unlock(&newmemory[position-NUM_BUFFERS].lock);
  2312. #endif
  2313. do {
  2314. #ifdef DEBUG
  2315. printf("Allocation Start : %lx\n", base_address);
  2316. #endif
  2317. map_address = (void *)-1;
  2318. func = &memoryalloc[0];
  2319. while ((func != NULL) && (map_address == (void *) -1)) {
  2320. map_address = (*func)((void *)base_address);
  2321. #ifdef ALLOC_DEVICEDRIVER
  2322. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  2323. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation was failed.\n");
  2324. }
  2325. #endif
  2326. #ifdef ALLOC_HUGETLBFILE
  2327. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  2328. #ifndef OS_WINDOWS
  2329. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  2330. #endif
  2331. }
  2332. #endif
  2333. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  2334. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  2335. #endif
  2336. func ++;
  2337. }
  2338. #ifdef DEBUG
  2339. printf(" Success -> %08lx\n", map_address);
  2340. #endif
  2341. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  2342. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  2343. } while ((BLASLONG)map_address == -1);
  2344. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2345. LOCK_COMMAND(&alloc_lock);
  2346. #endif
  2347. newmemory[position-NUM_BUFFERS].addr = map_address;
  2348. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2349. UNLOCK_COMMAND(&alloc_lock);
  2350. #endif
  2351. #ifdef DEBUG
  2352. printf(" Mapping Succeeded. %p(%d)\n", (void *)newmemory[position-NUM_BUFFERS].addr, position);
  2353. #endif
  2354. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2355. if (newmemory[position-NUM_BUFFERS].pos == -1) newmemory[position-NUM_BUFFERS].pos = mypos;
  2356. #endif
  2357. return (void *)newmemory[position-NUM_BUFFERS].addr;
  2358. terminate:
  2359. printf("OpenBLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  2360. printf("This library was built to support a maximum of %d threads - either rebuild OpenBLAS\n", NUM_BUFFERS);
  2361. printf("with a larger NUM_THREADS value or set the environment variable OPENBLAS_NUM_THREADS to\n");
  2362. printf("a sufficiently small number. This error typically occurs when the software that relies on\n");
  2363. printf("OpenBLAS calls BLAS functions from many threads in parallel, or when your computer has more\n");
  2364. printf("cpu cores than what OpenBLAS was configured to handle.\n");
  2365. return NULL;
  2366. }
  2367. void blas_memory_free(void *free_area){
  2368. int position;
  2369. #ifdef DEBUG
  2370. printf("Unmapped Start : %p ...\n", free_area);
  2371. #endif
  2372. position = 0;
  2373. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2374. LOCK_COMMAND(&alloc_lock);
  2375. #endif
  2376. while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
  2377. position++;
  2378. if (position >= NUM_BUFFERS && !memory_overflowed) goto error;
  2379. #ifdef DEBUG
  2380. if (memory[position].addr != free_area) goto error;
  2381. printf(" Position : %d\n", position);
  2382. #endif
  2383. if (unlikely(memory_overflowed && position >= NUM_BUFFERS)) {
  2384. while ((position < NUM_BUFFERS+512) && (newmemory[position-NUM_BUFFERS].addr != free_area))
  2385. position++;
  2386. // arm: ensure all writes are finished before other thread takes this memory
  2387. WMB;
  2388. newmemory[position].used = 0;
  2389. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2390. UNLOCK_COMMAND(&alloc_lock);
  2391. #endif
  2392. #ifdef DEBUG
  2393. printf("Unmap from overflow area succeeded.\n\n");
  2394. #endif
  2395. return;
  2396. } else {
  2397. // arm: ensure all writes are finished before other thread takes this memory
  2398. WMB;
  2399. memory[position].used = 0;
  2400. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2401. UNLOCK_COMMAND(&alloc_lock);
  2402. #endif
  2403. #ifdef DEBUG
  2404. printf("Unmap Succeeded.\n\n");
  2405. #endif
  2406. return;
  2407. }
  2408. error:
  2409. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  2410. #ifdef DEBUG
  2411. for (position = 0; position < NUM_BUFFERS; position++)
  2412. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  2413. #endif
  2414. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2415. UNLOCK_COMMAND(&alloc_lock);
  2416. #endif
  2417. return;
  2418. }
  2419. void *blas_memory_alloc_nolock(int unused) {
  2420. void *map_address;
  2421. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  2422. return map_address;
  2423. }
  2424. void blas_memory_free_nolock(void * map_address) {
  2425. free(map_address);
  2426. }
  2427. void blas_shutdown(void){
  2428. int pos;
  2429. #ifdef SMP
  2430. BLASFUNC(blas_thread_shutdown)();
  2431. #endif
  2432. LOCK_COMMAND(&alloc_lock);
  2433. for (pos = 0; pos < release_pos; pos ++) {
  2434. if (likely(pos < NUM_BUFFERS))
  2435. release_info[pos].func(&release_info[pos]);
  2436. else
  2437. new_release_info[pos-NUM_BUFFERS].func(&new_release_info[pos-NUM_BUFFERS]);
  2438. }
  2439. #ifdef SEEK_ADDRESS
  2440. base_address = 0UL;
  2441. #else
  2442. base_address = BASE_ADDRESS;
  2443. #endif
  2444. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  2445. memory[pos].addr = (void *)0;
  2446. memory[pos].used = 0;
  2447. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2448. memory[pos].pos = -1;
  2449. #endif
  2450. memory[pos].lock = 0;
  2451. }
  2452. if (memory_overflowed)
  2453. for (pos = 0; pos < 512; pos ++){
  2454. newmemory[pos].addr = (void *)0;
  2455. newmemory[pos].used = 0;
  2456. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2457. newmemory[pos].pos = -1;
  2458. #endif
  2459. newmemory[pos].lock = 0;
  2460. }
  2461. UNLOCK_COMMAND(&alloc_lock);
  2462. return;
  2463. }
  2464. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2465. #if defined(SMP) || defined(USE_LOCKING)
  2466. #if defined(USE_PTHREAD_LOCK)
  2467. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  2468. #elif defined(USE_PTHREAD_SPINLOCK)
  2469. static pthread_spinlock_t init_lock = 0;
  2470. #else
  2471. static BLASULONG init_lock = 0UL;
  2472. #endif
  2473. #endif
  2474. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  2475. void *sa, void *sb, BLASLONG pos) {
  2476. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  2477. size_t size;
  2478. BLASULONG buffer;
  2479. size = BUFFER_SIZE - PAGESIZE;
  2480. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2481. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2482. if (hot_alloc != 2) {
  2483. #endif
  2484. #if defined(SMP) || defined(USE_LOCKING)
  2485. LOCK_COMMAND(&init_lock);
  2486. #endif
  2487. while (size > 0) {
  2488. *(int *)buffer = size;
  2489. buffer += PAGESIZE;
  2490. size -= PAGESIZE;
  2491. }
  2492. #if defined(SMP) || defined(USE_LOCKING)
  2493. UNLOCK_COMMAND(&init_lock);
  2494. #endif
  2495. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  2496. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2497. while (size > 0) {
  2498. *(int *)buffer = size;
  2499. buffer += 64;
  2500. size -= 64;
  2501. }
  2502. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2503. }
  2504. #endif
  2505. #endif
  2506. }
  2507. #ifdef SMP
  2508. static void _init_thread_memory(void *buffer) {
  2509. blas_queue_t queue[MAX_CPU_NUMBER];
  2510. int num_cpu;
  2511. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  2512. blas_queue_init(&queue[num_cpu]);
  2513. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  2514. queue[num_cpu].routine = &_touch_memory;
  2515. queue[num_cpu].args = NULL;
  2516. queue[num_cpu].next = &queue[num_cpu + 1];
  2517. }
  2518. queue[num_cpu - 1].next = NULL;
  2519. queue[0].sa = buffer;
  2520. exec_blas(num_cpu, queue);
  2521. }
  2522. #endif
  2523. static void gotoblas_memory_init(void) {
  2524. void *buffer;
  2525. hot_alloc = 1;
  2526. buffer = (void *)blas_memory_alloc(0);
  2527. #ifdef SMP
  2528. if (blas_cpu_number == 0) blas_get_cpu_number();
  2529. #ifdef SMP_SERVER
  2530. if (blas_server_avail == 0) blas_thread_init();
  2531. #endif
  2532. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  2533. #else
  2534. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  2535. #endif
  2536. blas_memory_free(buffer);
  2537. }
  2538. #endif
  2539. /* Initialization for all function; this function should be called before main */
  2540. static int gotoblas_initialized = 0;
  2541. extern void openblas_read_env();
  2542. void CONSTRUCTOR gotoblas_init(void) {
  2543. if (gotoblas_initialized) return;
  2544. #ifdef SMP
  2545. openblas_fork_handler();
  2546. #endif
  2547. openblas_read_env();
  2548. #ifdef PROFILE
  2549. moncontrol (0);
  2550. #endif
  2551. #ifdef DYNAMIC_ARCH
  2552. gotoblas_dynamic_init();
  2553. #endif
  2554. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2555. gotoblas_affinity_init();
  2556. #endif
  2557. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2558. gotoblas_memory_init();
  2559. #endif
  2560. //#if defined(OS_LINUX)
  2561. #if 0
  2562. struct rlimit curlimit;
  2563. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  2564. {
  2565. if ( curlimit.rlim_cur != curlimit.rlim_max )
  2566. {
  2567. curlimit.rlim_cur = curlimit.rlim_max;
  2568. setrlimit(RLIMIT_STACK, &curlimit);
  2569. }
  2570. }
  2571. #endif
  2572. #ifdef SMP
  2573. if (blas_cpu_number == 0) blas_get_cpu_number();
  2574. #ifdef SMP_SERVER
  2575. if (blas_server_avail == 0) blas_thread_init();
  2576. #endif
  2577. #endif
  2578. #ifdef FUNCTION_PROFILE
  2579. gotoblas_profile_init();
  2580. #endif
  2581. gotoblas_initialized = 1;
  2582. #ifdef PROFILE
  2583. moncontrol (1);
  2584. #endif
  2585. }
  2586. void DESTRUCTOR gotoblas_quit(void) {
  2587. if (gotoblas_initialized == 0) return;
  2588. blas_shutdown();
  2589. #ifdef PROFILE
  2590. moncontrol (0);
  2591. #endif
  2592. #ifdef FUNCTION_PROFILE
  2593. gotoblas_profile_quit();
  2594. #endif
  2595. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2596. gotoblas_affinity_quit();
  2597. #endif
  2598. #ifdef DYNAMIC_ARCH
  2599. gotoblas_dynamic_quit();
  2600. #endif
  2601. gotoblas_initialized = 0;
  2602. #ifdef PROFILE
  2603. moncontrol (1);
  2604. #endif
  2605. }
  2606. #if defined(_MSC_VER) && !defined(__clang__)
  2607. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  2608. {
  2609. switch (ul_reason_for_call)
  2610. {
  2611. case DLL_PROCESS_ATTACH:
  2612. gotoblas_init();
  2613. break;
  2614. case DLL_THREAD_ATTACH:
  2615. break;
  2616. case DLL_THREAD_DETACH:
  2617. break;
  2618. case DLL_PROCESS_DETACH:
  2619. gotoblas_quit();
  2620. break;
  2621. default:
  2622. break;
  2623. }
  2624. return TRUE;
  2625. }
  2626. /*
  2627. This is to allow static linking.
  2628. Code adapted from Google performance tools:
  2629. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  2630. Reference:
  2631. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  2632. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  2633. */
  2634. static int on_process_term(void)
  2635. {
  2636. gotoblas_quit();
  2637. return 0;
  2638. }
  2639. #ifdef _WIN64
  2640. #pragma comment(linker, "/INCLUDE:_tls_used")
  2641. #else
  2642. #pragma comment(linker, "/INCLUDE:__tls_used")
  2643. #endif
  2644. #ifdef _WIN64
  2645. #pragma const_seg(".CRT$XLB")
  2646. #else
  2647. #pragma data_seg(".CRT$XLB")
  2648. #endif
  2649. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  2650. #ifdef _WIN64
  2651. #pragma const_seg()
  2652. #else
  2653. #pragma data_seg()
  2654. #endif
  2655. #ifdef _WIN64
  2656. #pragma const_seg(".CRT$XTU")
  2657. #else
  2658. #pragma data_seg(".CRT$XTU")
  2659. #endif
  2660. static int(*p_process_term)(void) = on_process_term;
  2661. #ifdef _WIN64
  2662. #pragma const_seg()
  2663. #else
  2664. #pragma data_seg()
  2665. #endif
  2666. #endif
  2667. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  2668. /* Don't call me; this is just work around for PGI / Sun bug */
  2669. void gotoblas_dummy_for_PGI(void) {
  2670. gotoblas_init();
  2671. gotoblas_quit();
  2672. #if __PGIC__ < 19
  2673. #if 0
  2674. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  2675. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  2676. #else
  2677. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  2678. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  2679. #endif
  2680. #endif
  2681. }
  2682. #endif
  2683. #endif