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