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 76 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
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253
  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. cpu_set_t cpuset,*cpusetp;
  190. size_t size;
  191. int ret;
  192. #if defined(__GLIBC_PREREQ)
  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)
  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)
  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)
  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 memory regions.\n");
  991. return NULL;
  992. }
  993. void blas_memory_free(void *buffer){
  994. #ifdef DEBUG
  995. int position;
  996. struct alloc_t ** alloc_table;
  997. #endif
  998. /* Since we passed an offset pointer to the caller, get back to the actual allocation */
  999. struct alloc_t *alloc_info = (void *)(((char *)buffer) - sizeof(struct alloc_t));
  1000. #ifdef DEBUG
  1001. printf("Unmapped Start : %p ...\n", alloc_info);
  1002. #endif
  1003. alloc_info->used = 0;
  1004. #ifdef DEBUG
  1005. printf("Unmap Succeeded.\n\n");
  1006. #endif
  1007. return;
  1008. #ifdef DEBUG
  1009. alloc_table = get_memory_table();
  1010. for (position = 0; position < NUM_BUFFERS; position++){
  1011. if (alloc_table[position]) {
  1012. printf("%4ld %p : %d\n", position, alloc_table[position], alloc_table[position]->used);
  1013. }
  1014. }
  1015. #endif
  1016. return;
  1017. }
  1018. void *blas_memory_alloc_nolock(int unused) {
  1019. void *map_address;
  1020. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1021. return map_address;
  1022. }
  1023. void blas_memory_free_nolock(void * map_address) {
  1024. free(map_address);
  1025. }
  1026. #ifdef SMP
  1027. void blas_thread_memory_cleanup(void) {
  1028. blas_memory_cleanup((void*)get_memory_table());
  1029. }
  1030. #endif
  1031. void blas_shutdown(void){
  1032. #ifdef SMP
  1033. BLASFUNC(blas_thread_shutdown)();
  1034. #endif
  1035. #ifdef SMP
  1036. /* Only cleanupIf we were built for threading and TLS was initialized */
  1037. if (local_storage_key)
  1038. #endif
  1039. blas_thread_memory_cleanup();
  1040. #ifdef SEEK_ADDRESS
  1041. base_address = 0UL;
  1042. #else
  1043. base_address = BASE_ADDRESS;
  1044. #endif
  1045. return;
  1046. }
  1047. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1048. #ifdef SMP
  1049. #if defined(USE_PTHREAD_LOCK)
  1050. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  1051. #elif defined(USE_PTHREAD_SPINLOCK)
  1052. static pthread_spinlock_t init_lock = 0;
  1053. #else
  1054. static BLASULONG init_lock = 0UL;
  1055. #endif
  1056. #endif
  1057. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  1058. void *sa, void *sb, BLASLONG pos) {
  1059. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  1060. size_t size;
  1061. BLASULONG buffer;
  1062. size = allocation_block_size - PAGESIZE;
  1063. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1064. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1065. if (hot_alloc != 2) {
  1066. #endif
  1067. #ifdef SMP
  1068. LOCK_COMMAND(&init_lock);
  1069. #endif
  1070. while (size > 0) {
  1071. *(int *)buffer = size;
  1072. buffer += PAGESIZE;
  1073. size -= PAGESIZE;
  1074. }
  1075. #ifdef SMP
  1076. UNLOCK_COMMAND(&init_lock);
  1077. #endif
  1078. size = MIN((allocation_block_size - PAGESIZE), L2_SIZE);
  1079. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1080. while (size > 0) {
  1081. *(int *)buffer = size;
  1082. buffer += 64;
  1083. size -= 64;
  1084. }
  1085. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1086. }
  1087. #endif
  1088. #endif
  1089. }
  1090. #ifdef SMP
  1091. static void _init_thread_memory(void *buffer) {
  1092. blas_queue_t queue[MAX_CPU_NUMBER];
  1093. int num_cpu;
  1094. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  1095. blas_queue_init(&queue[num_cpu]);
  1096. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  1097. queue[num_cpu].routine = &_touch_memory;
  1098. queue[num_cpu].args = NULL;
  1099. queue[num_cpu].next = &queue[num_cpu + 1];
  1100. }
  1101. queue[num_cpu - 1].next = NULL;
  1102. queue[0].sa = buffer;
  1103. exec_blas(num_cpu, queue);
  1104. }
  1105. #endif
  1106. static void gotoblas_memory_init(void) {
  1107. void *buffer;
  1108. hot_alloc = 1;
  1109. buffer = (void *)blas_memory_alloc(0);
  1110. #ifdef SMP
  1111. if (blas_cpu_number == 0) blas_get_cpu_number();
  1112. #ifdef SMP_SERVER
  1113. if (blas_server_avail == 0) blas_thread_init();
  1114. #endif
  1115. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  1116. #else
  1117. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  1118. #endif
  1119. blas_memory_free(buffer);
  1120. }
  1121. #endif
  1122. /* Initialization for all function; this function should be called before main */
  1123. static int gotoblas_initialized = 0;
  1124. extern void openblas_read_env();
  1125. void CONSTRUCTOR gotoblas_init(void) {
  1126. if (gotoblas_initialized) return;
  1127. #ifdef SMP
  1128. openblas_fork_handler();
  1129. #endif
  1130. openblas_read_env();
  1131. #ifdef PROFILE
  1132. moncontrol (0);
  1133. #endif
  1134. #ifdef DYNAMIC_ARCH
  1135. gotoblas_dynamic_init();
  1136. #endif
  1137. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1138. gotoblas_affinity_init();
  1139. #endif
  1140. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1141. gotoblas_memory_init();
  1142. #endif
  1143. //#if defined(OS_LINUX)
  1144. #if 0
  1145. struct rlimit curlimit;
  1146. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  1147. {
  1148. if ( curlimit.rlim_cur != curlimit.rlim_max )
  1149. {
  1150. curlimit.rlim_cur = curlimit.rlim_max;
  1151. setrlimit(RLIMIT_STACK, &curlimit);
  1152. }
  1153. }
  1154. #endif
  1155. #ifdef SMP
  1156. if (blas_cpu_number == 0) blas_get_cpu_number();
  1157. #ifdef SMP_SERVER
  1158. if (blas_server_avail == 0) blas_thread_init();
  1159. #endif
  1160. #endif
  1161. #ifdef FUNCTION_PROFILE
  1162. gotoblas_profile_init();
  1163. #endif
  1164. gotoblas_initialized = 1;
  1165. #ifdef PROFILE
  1166. moncontrol (1);
  1167. #endif
  1168. }
  1169. void DESTRUCTOR gotoblas_quit(void) {
  1170. if (gotoblas_initialized == 0) return;
  1171. blas_shutdown();
  1172. #if defined(SMP)
  1173. #if defined(OS_WINDOWS)
  1174. TlsFree(local_storage_key);
  1175. #else
  1176. pthread_key_delete(local_storage_key);
  1177. #endif
  1178. #endif
  1179. #ifdef PROFILE
  1180. moncontrol (0);
  1181. #endif
  1182. #ifdef FUNCTION_PROFILE
  1183. gotoblas_profile_quit();
  1184. #endif
  1185. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1186. gotoblas_affinity_quit();
  1187. #endif
  1188. #ifdef DYNAMIC_ARCH
  1189. gotoblas_dynamic_quit();
  1190. #endif
  1191. gotoblas_initialized = 0;
  1192. #ifdef PROFILE
  1193. moncontrol (1);
  1194. #endif
  1195. }
  1196. #if defined(_MSC_VER) && !defined(__clang__)
  1197. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  1198. {
  1199. switch (ul_reason_for_call)
  1200. {
  1201. case DLL_PROCESS_ATTACH:
  1202. gotoblas_init();
  1203. break;
  1204. case DLL_THREAD_ATTACH:
  1205. break;
  1206. case DLL_THREAD_DETACH:
  1207. #if defined(SMP)
  1208. blas_thread_memory_cleanup();
  1209. #endif
  1210. break;
  1211. case DLL_PROCESS_DETACH:
  1212. gotoblas_quit();
  1213. break;
  1214. default:
  1215. break;
  1216. }
  1217. return TRUE;
  1218. }
  1219. /*
  1220. This is to allow static linking.
  1221. Code adapted from Google performance tools:
  1222. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  1223. Reference:
  1224. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  1225. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  1226. */
  1227. static int on_process_term(void)
  1228. {
  1229. gotoblas_quit();
  1230. return 0;
  1231. }
  1232. #ifdef _WIN64
  1233. #pragma comment(linker, "/INCLUDE:_tls_used")
  1234. #else
  1235. #pragma comment(linker, "/INCLUDE:__tls_used")
  1236. #endif
  1237. #ifdef _WIN64
  1238. #pragma const_seg(".CRT$XLB")
  1239. #else
  1240. #pragma data_seg(".CRT$XLB")
  1241. #endif
  1242. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  1243. #ifdef _WIN64
  1244. #pragma const_seg()
  1245. #else
  1246. #pragma data_seg()
  1247. #endif
  1248. #ifdef _WIN64
  1249. #pragma const_seg(".CRT$XTU")
  1250. #else
  1251. #pragma data_seg(".CRT$XTU")
  1252. #endif
  1253. static int(*p_process_term)(void) = on_process_term;
  1254. #ifdef _WIN64
  1255. #pragma const_seg()
  1256. #else
  1257. #pragma data_seg()
  1258. #endif
  1259. #endif
  1260. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  1261. /* Don't call me; this is just work around for PGI / Sun bug */
  1262. void gotoblas_dummy_for_PGI(void) {
  1263. gotoblas_init();
  1264. gotoblas_quit();
  1265. #if __PGIC__ < 19
  1266. #if 0
  1267. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  1268. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  1269. #else
  1270. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  1271. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  1272. #endif
  1273. #endif
  1274. }
  1275. #endif
  1276. #else
  1277. /* USE_TLS / COMPILE_TLS not set */
  1278. #include <errno.h>
  1279. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  1280. #define ALLOC_WINDOWS
  1281. #ifndef MEM_LARGE_PAGES
  1282. #define MEM_LARGE_PAGES 0x20000000
  1283. #endif
  1284. #elif !defined(OS_EMBEDDED)
  1285. #define ALLOC_MMAP
  1286. #define ALLOC_MALLOC
  1287. #else
  1288. #define ALLOC_MALLOC
  1289. #endif
  1290. #include <stdlib.h>
  1291. #include <stdio.h>
  1292. #include <fcntl.h>
  1293. #if (!defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)) && !defined(OS_EMBEDDED)
  1294. #include <sys/mman.h>
  1295. #ifndef NO_SYSV_IPC
  1296. #include <sys/shm.h>
  1297. #endif
  1298. #include <sys/ipc.h>
  1299. #endif
  1300. #include <sys/types.h>
  1301. #ifdef OS_LINUX
  1302. #include <sys/sysinfo.h>
  1303. #include <sched.h>
  1304. #include <errno.h>
  1305. #include <linux/unistd.h>
  1306. #include <sys/syscall.h>
  1307. #include <sys/time.h>
  1308. #include <sys/resource.h>
  1309. #endif
  1310. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
  1311. #include <sys/sysctl.h>
  1312. #include <sys/resource.h>
  1313. #endif
  1314. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  1315. #include <conio.h>
  1316. #undef printf
  1317. #define printf _cprintf
  1318. #endif
  1319. #ifdef OS_LINUX
  1320. #ifndef MPOL_PREFERRED
  1321. #define MPOL_PREFERRED 1
  1322. #endif
  1323. #endif
  1324. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  1325. #define NO_WARMUP
  1326. #endif
  1327. #ifndef SHM_HUGETLB
  1328. #define SHM_HUGETLB 04000
  1329. #endif
  1330. #ifndef FIXED_PAGESIZE
  1331. #define FIXED_PAGESIZE 4096
  1332. #endif
  1333. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  1334. #if defined(_MSC_VER) && !defined(__clang__)
  1335. #define CONSTRUCTOR __cdecl
  1336. #define DESTRUCTOR __cdecl
  1337. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  1338. #define CONSTRUCTOR __attribute__ ((constructor))
  1339. #define DESTRUCTOR __attribute__ ((destructor))
  1340. #elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
  1341. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  1342. #define DESTRUCTOR __attribute__ ((destructor(101)))
  1343. #else
  1344. #define CONSTRUCTOR __attribute__ ((constructor))
  1345. #define DESTRUCTOR __attribute__ ((destructor))
  1346. #endif
  1347. #ifdef DYNAMIC_ARCH
  1348. gotoblas_t *gotoblas = NULL;
  1349. #endif
  1350. extern void openblas_warning(int verbose, const char * msg);
  1351. #ifndef SMP
  1352. #define blas_cpu_number 1
  1353. #define blas_num_threads 1
  1354. /* Dummy Function */
  1355. int goto_get_num_procs (void) { return 1;};
  1356. void goto_set_num_threads(int num_threads) {};
  1357. #else
  1358. #if defined(OS_LINUX) || defined(OS_SUNOS)
  1359. #ifndef NO_AFFINITY
  1360. int get_num_procs(void);
  1361. #else
  1362. int get_num_procs(void) {
  1363. static int nums = 0;
  1364. #if defined(__GLIBC_PREREQ)
  1365. cpu_set_t cpuset,*cpusetp;
  1366. size_t size;
  1367. int ret;
  1368. #if !__GLIBC_PREREQ(2, 7)
  1369. int i;
  1370. #if !__GLIBC_PREREQ(2, 6)
  1371. int n;
  1372. #endif
  1373. #endif
  1374. #endif
  1375. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1376. #if !defined(OS_LINUX)
  1377. return nums;
  1378. #endif
  1379. #if !defined(__GLIBC_PREREQ)
  1380. return nums;
  1381. #else
  1382. #if !__GLIBC_PREREQ(2, 3)
  1383. return nums;
  1384. #endif
  1385. #if !__GLIBC_PREREQ(2, 7)
  1386. ret = sched_getaffinity(0,sizeof(cpuset), &cpuset);
  1387. if (ret!=0) return nums;
  1388. n=0;
  1389. #if !__GLIBC_PREREQ(2, 6)
  1390. for (i=0;i<nums;i++)
  1391. if (CPU_ISSET(i,&cpuset)) n++;
  1392. nums=n;
  1393. #else
  1394. nums = CPU_COUNT(sizeof(cpuset),&cpuset);
  1395. #endif
  1396. return nums;
  1397. #else
  1398. if (nums >= CPU_SETSIZE) {
  1399. cpusetp = CPU_ALLOC(nums);
  1400. if (cpusetp == NULL) {
  1401. return nums;
  1402. }
  1403. size = CPU_ALLOC_SIZE(nums);
  1404. ret = sched_getaffinity(0,size,cpusetp);
  1405. if (ret!=0) {
  1406. CPU_FREE(cpusetp);
  1407. return nums;
  1408. }
  1409. ret = CPU_COUNT_S(size,cpusetp);
  1410. if (ret > 0 && ret < nums) nums = ret;
  1411. CPU_FREE(cpusetp);
  1412. return nums;
  1413. } else {
  1414. ret = sched_getaffinity(0,sizeof(cpuset),&cpuset);
  1415. if (ret!=0) {
  1416. return nums;
  1417. }
  1418. ret = CPU_COUNT(&cpuset);
  1419. if (ret > 0 && ret < nums) nums = ret;
  1420. return nums;
  1421. }
  1422. #endif
  1423. #endif
  1424. }
  1425. #endif
  1426. #endif
  1427. #ifdef OS_ANDROID
  1428. int get_num_procs(void) {
  1429. static int nums = 0;
  1430. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1431. return nums;
  1432. }
  1433. #endif
  1434. #ifdef OS_HAIKU
  1435. int get_num_procs(void) {
  1436. static int nums = 0;
  1437. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1438. return nums;
  1439. }
  1440. #endif
  1441. #ifdef OS_AIX
  1442. int get_num_procs(void) {
  1443. static int nums = 0;
  1444. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1445. return nums;
  1446. }
  1447. #endif
  1448. #ifdef OS_WINDOWS
  1449. int get_num_procs(void) {
  1450. static int nums = 0;
  1451. if (nums == 0) {
  1452. SYSTEM_INFO sysinfo;
  1453. GetSystemInfo(&sysinfo);
  1454. nums = sysinfo.dwNumberOfProcessors;
  1455. }
  1456. return nums;
  1457. }
  1458. #endif
  1459. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY)
  1460. int get_num_procs(void) {
  1461. static int nums = 0;
  1462. int m[2];
  1463. size_t len;
  1464. if (nums == 0) {
  1465. m[0] = CTL_HW;
  1466. m[1] = HW_NCPU;
  1467. len = sizeof(int);
  1468. sysctl(m, 2, &nums, &len, NULL, 0);
  1469. }
  1470. return nums;
  1471. }
  1472. #endif
  1473. #if defined(OS_DARWIN)
  1474. int get_num_procs(void) {
  1475. static int nums = 0;
  1476. size_t len;
  1477. if (nums == 0){
  1478. len = sizeof(int);
  1479. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  1480. }
  1481. return nums;
  1482. }
  1483. /*
  1484. void set_stack_limit(int limitMB){
  1485. int result=0;
  1486. struct rlimit rl;
  1487. rlim_t StackSize;
  1488. StackSize=limitMB*1024*1024;
  1489. result=getrlimit(RLIMIT_STACK, &rl);
  1490. if(result==0){
  1491. if(rl.rlim_cur < StackSize){
  1492. rl.rlim_cur=StackSize;
  1493. result=setrlimit(RLIMIT_STACK, &rl);
  1494. if(result !=0){
  1495. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  1496. }
  1497. }
  1498. }
  1499. }
  1500. */
  1501. #endif
  1502. /*
  1503. OpenBLAS uses the numbers of CPU cores in multithreading.
  1504. It can be set by openblas_set_num_threads(int num_threads);
  1505. */
  1506. int blas_cpu_number = 0;
  1507. /*
  1508. The numbers of threads in the thread pool.
  1509. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  1510. */
  1511. int blas_num_threads = 0;
  1512. int goto_get_num_procs (void) {
  1513. return blas_cpu_number;
  1514. }
  1515. void openblas_fork_handler()
  1516. {
  1517. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  1518. // built with "make USE_OPENMP=0".
  1519. // Hanging can still happen when OpenBLAS is built against the libgomp
  1520. // implementation of OpenMP. The problem is tracked at:
  1521. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  1522. // In the mean time build with USE_OPENMP=0 or link against another
  1523. // implementation of OpenMP.
  1524. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  1525. int err;
  1526. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
  1527. if(err != 0)
  1528. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  1529. #endif
  1530. }
  1531. extern int openblas_num_threads_env();
  1532. extern int openblas_goto_num_threads_env();
  1533. extern int openblas_omp_num_threads_env();
  1534. int blas_get_cpu_number(void){
  1535. #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)
  1536. int max_num;
  1537. #endif
  1538. int blas_goto_num = 0;
  1539. int blas_omp_num = 0;
  1540. if (blas_num_threads) return blas_num_threads;
  1541. #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)
  1542. max_num = get_num_procs();
  1543. #endif
  1544. // blas_goto_num = 0;
  1545. #ifndef USE_OPENMP
  1546. blas_goto_num=openblas_num_threads_env();
  1547. if (blas_goto_num < 0) blas_goto_num = 0;
  1548. if (blas_goto_num == 0) {
  1549. blas_goto_num=openblas_goto_num_threads_env();
  1550. if (blas_goto_num < 0) blas_goto_num = 0;
  1551. }
  1552. #endif
  1553. // blas_omp_num = 0;
  1554. blas_omp_num=openblas_omp_num_threads_env();
  1555. if (blas_omp_num < 0) blas_omp_num = 0;
  1556. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  1557. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  1558. else blas_num_threads = MAX_CPU_NUMBER;
  1559. #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)
  1560. if (blas_num_threads > max_num) blas_num_threads = max_num;
  1561. #endif
  1562. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  1563. #ifdef DEBUG
  1564. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  1565. #endif
  1566. blas_cpu_number = blas_num_threads;
  1567. return blas_num_threads;
  1568. }
  1569. #endif
  1570. int openblas_get_num_procs(void) {
  1571. #ifndef SMP
  1572. return 1;
  1573. #else
  1574. return get_num_procs();
  1575. #endif
  1576. }
  1577. int openblas_get_num_threads(void) {
  1578. #ifndef SMP
  1579. return 1;
  1580. #else
  1581. // init blas_cpu_number if needed
  1582. blas_get_cpu_number();
  1583. return blas_cpu_number;
  1584. #endif
  1585. }
  1586. struct release_t {
  1587. void *address;
  1588. void (*func)(struct release_t *);
  1589. long attr;
  1590. };
  1591. int hugetlb_allocated = 0;
  1592. static struct release_t release_info[NUM_BUFFERS];
  1593. static int release_pos = 0;
  1594. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1595. static int hot_alloc = 0;
  1596. #endif
  1597. /* Global lock for memory allocation */
  1598. #if defined(USE_PTHREAD_LOCK)
  1599. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  1600. #elif defined(USE_PTHREAD_SPINLOCK)
  1601. static pthread_spinlock_t alloc_lock = 0;
  1602. #else
  1603. static BLASULONG alloc_lock = 0UL;
  1604. #endif
  1605. #ifdef ALLOC_MMAP
  1606. static void alloc_mmap_free(struct release_t *release){
  1607. if (!release->address) return;
  1608. if (munmap(release -> address, BUFFER_SIZE)) {
  1609. int errsv=errno;
  1610. perror("OpenBLAS : munmap failed:");
  1611. printf("error code=%d,\trelease->address=%p\n",errsv,release->address);
  1612. }
  1613. }
  1614. #ifdef NO_WARMUP
  1615. static void *alloc_mmap(void *address){
  1616. void *map_address;
  1617. if (address){
  1618. map_address = mmap(address,
  1619. BUFFER_SIZE,
  1620. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1621. } else {
  1622. map_address = mmap(address,
  1623. BUFFER_SIZE,
  1624. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1625. }
  1626. if (map_address != (void *)-1) {
  1627. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1628. LOCK_COMMAND(&alloc_lock);
  1629. #endif
  1630. release_info[release_pos].address = map_address;
  1631. release_info[release_pos].func = alloc_mmap_free;
  1632. release_pos ++;
  1633. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1634. UNLOCK_COMMAND(&alloc_lock);
  1635. #endif
  1636. } else {
  1637. #ifdef DEBUG
  1638. int errsv=errno;
  1639. perror("OpenBLAS : mmap failed:");
  1640. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1641. #endif
  1642. }
  1643. #ifdef OS_LINUX
  1644. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1645. #endif
  1646. return map_address;
  1647. }
  1648. #else
  1649. #define BENCH_ITERATION 4
  1650. #define SCALING 2
  1651. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  1652. BLASULONG original, *p;
  1653. BLASULONG start, stop, min;
  1654. int iter, i, count;
  1655. min = (BLASULONG)-1;
  1656. original = *(BLASULONG *)(address + size - PAGESIZE);
  1657. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  1658. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  1659. p = (BLASULONG *)address;
  1660. count = size / PAGESIZE;
  1661. start = rpcc();
  1662. for (i = 0; i < count; i ++) {
  1663. p = (BLASULONG *)(*p);
  1664. }
  1665. stop = rpcc();
  1666. if (min > stop - start) min = stop - start;
  1667. }
  1668. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  1669. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  1670. return min;
  1671. }
  1672. static void *alloc_mmap(void *address){
  1673. void *map_address, *best_address;
  1674. BLASULONG best, start, current;
  1675. BLASULONG allocsize;
  1676. if (address){
  1677. /* Just give up use advanced operation */
  1678. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1679. #ifdef OS_LINUX
  1680. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1681. #endif
  1682. } else {
  1683. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1684. if (hot_alloc == 0) {
  1685. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1686. #ifdef OS_LINUX
  1687. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1688. #endif
  1689. } else {
  1690. #endif
  1691. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  1692. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1693. if (map_address != (void *)-1) {
  1694. #ifdef OS_LINUX
  1695. #ifdef DEBUG
  1696. int ret=0;
  1697. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1698. if(ret==-1){
  1699. int errsv=errno;
  1700. perror("OpenBLAS alloc_mmap:");
  1701. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1702. }
  1703. #else
  1704. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1705. #endif
  1706. #endif
  1707. #ifdef BUILD_DOUBLE
  1708. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  1709. #elif defined(BUILD_COMPLEX16)
  1710. allocsize = ZGEMM_P * ZGEMM_Q * sizeof(double);
  1711. #elif defined(BUILD_COMPLEX)
  1712. allocsize = CGEMM_P * CGEMM_Q * sizeof(double);
  1713. #else
  1714. allocsize = SGEMM_P * SGEMM_Q * sizeof(double);
  1715. #endif
  1716. start = (BLASULONG)map_address;
  1717. current = (SCALING - 1) * BUFFER_SIZE;
  1718. while(current > 0) {
  1719. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  1720. start += PAGESIZE;
  1721. current -= PAGESIZE;
  1722. }
  1723. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  1724. start = (BLASULONG)map_address;
  1725. best = (BLASULONG)-1;
  1726. best_address = map_address;
  1727. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  1728. current = run_bench(start, allocsize);
  1729. if (best > current) {
  1730. best = current;
  1731. best_address = (void *)start;
  1732. }
  1733. start += PAGESIZE;
  1734. }
  1735. if ((BLASULONG)best_address > (BLASULONG)map_address)
  1736. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  1737. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  1738. map_address = best_address;
  1739. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1740. hot_alloc = 2;
  1741. #endif
  1742. }
  1743. }
  1744. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1745. }
  1746. #endif
  1747. if (map_address != (void *)-1) {
  1748. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1749. LOCK_COMMAND(&alloc_lock);
  1750. #endif
  1751. release_info[release_pos].address = map_address;
  1752. release_info[release_pos].func = alloc_mmap_free;
  1753. release_pos ++;
  1754. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1755. UNLOCK_COMMAND(&alloc_lock);
  1756. #endif
  1757. }
  1758. return map_address;
  1759. }
  1760. #endif
  1761. #endif
  1762. #ifdef ALLOC_MALLOC
  1763. static void alloc_malloc_free(struct release_t *release){
  1764. free(release -> address);
  1765. }
  1766. static void *alloc_malloc(void *address){
  1767. void *map_address;
  1768. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1769. if (map_address == (void *)NULL) map_address = (void *)-1;
  1770. if (map_address != (void *)-1) {
  1771. release_info[release_pos].address = map_address;
  1772. release_info[release_pos].func = alloc_malloc_free;
  1773. release_pos ++;
  1774. }
  1775. return map_address;
  1776. }
  1777. #endif
  1778. #ifdef ALLOC_QALLOC
  1779. void *qalloc(int flags, size_t bytes);
  1780. void *qfree (void *address);
  1781. #define QNONCACHE 0x1
  1782. #define QCOMMS 0x2
  1783. #define QFAST 0x4
  1784. static void alloc_qalloc_free(struct release_t *release){
  1785. qfree(release -> address);
  1786. }
  1787. static void *alloc_qalloc(void *address){
  1788. void *map_address;
  1789. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  1790. if (map_address == (void *)NULL) map_address = (void *)-1;
  1791. if (map_address != (void *)-1) {
  1792. release_info[release_pos].address = map_address;
  1793. release_info[release_pos].func = alloc_qalloc_free;
  1794. release_pos ++;
  1795. }
  1796. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  1797. }
  1798. #endif
  1799. #ifdef ALLOC_WINDOWS
  1800. static void alloc_windows_free(struct release_t *release){
  1801. VirtualFree(release -> address, 0, MEM_RELEASE);
  1802. }
  1803. static void *alloc_windows(void *address){
  1804. void *map_address;
  1805. map_address = VirtualAlloc(address,
  1806. BUFFER_SIZE,
  1807. MEM_RESERVE | MEM_COMMIT,
  1808. PAGE_READWRITE);
  1809. if (map_address == (void *)NULL) map_address = (void *)-1;
  1810. if (map_address != (void *)-1) {
  1811. release_info[release_pos].address = map_address;
  1812. release_info[release_pos].func = alloc_windows_free;
  1813. release_pos ++;
  1814. }
  1815. return map_address;
  1816. }
  1817. #endif
  1818. #ifdef ALLOC_DEVICEDRIVER
  1819. #ifndef DEVICEDRIVER_NAME
  1820. #define DEVICEDRIVER_NAME "/dev/mapper"
  1821. #endif
  1822. static void alloc_devicedirver_free(struct release_t *release){
  1823. if (munmap(release -> address, BUFFER_SIZE)) {
  1824. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  1825. }
  1826. if (close(release -> attr)) {
  1827. printf("OpenBLAS : Bugphysarea close failed.\n");
  1828. }
  1829. }
  1830. static void *alloc_devicedirver(void *address){
  1831. int fd;
  1832. void *map_address;
  1833. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  1834. return (void *)-1;
  1835. }
  1836. map_address = mmap(address, BUFFER_SIZE,
  1837. PROT_READ | PROT_WRITE,
  1838. MAP_FILE | MAP_SHARED,
  1839. fd, 0);
  1840. if (map_address != (void *)-1) {
  1841. release_info[release_pos].address = map_address;
  1842. release_info[release_pos].attr = fd;
  1843. release_info[release_pos].func = alloc_devicedirver_free;
  1844. release_pos ++;
  1845. }
  1846. return map_address;
  1847. }
  1848. #endif
  1849. #ifdef ALLOC_SHM
  1850. static void alloc_shm_free(struct release_t *release){
  1851. if (shmdt(release -> address)) {
  1852. printf("OpenBLAS : Shared memory unmap failed.\n");
  1853. }
  1854. }
  1855. static void *alloc_shm(void *address){
  1856. void *map_address;
  1857. int shmid;
  1858. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  1859. map_address = (void *)shmat(shmid, address, 0);
  1860. if (map_address != (void *)-1){
  1861. #ifdef OS_LINUX
  1862. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1863. #endif
  1864. shmctl(shmid, IPC_RMID, 0);
  1865. release_info[release_pos].address = map_address;
  1866. release_info[release_pos].attr = shmid;
  1867. release_info[release_pos].func = alloc_shm_free;
  1868. release_pos ++;
  1869. }
  1870. return map_address;
  1871. }
  1872. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  1873. static void alloc_hugetlb_free(struct release_t *release){
  1874. #if defined(OS_LINUX) || defined(OS_AIX)
  1875. if (shmdt(release -> address)) {
  1876. printf("OpenBLAS : Hugepage unmap failed.\n");
  1877. }
  1878. #endif
  1879. #ifdef __sun__
  1880. munmap(release -> address, BUFFER_SIZE);
  1881. #endif
  1882. #ifdef OS_WINDOWS
  1883. VirtualFree(release -> address, 0, MEM_LARGE_PAGES | MEM_RELEASE);
  1884. #endif
  1885. }
  1886. static void *alloc_hugetlb(void *address){
  1887. void *map_address = (void *)-1;
  1888. #if defined(OS_LINUX) || defined(OS_AIX)
  1889. int shmid;
  1890. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  1891. #ifdef OS_LINUX
  1892. SHM_HUGETLB |
  1893. #endif
  1894. #ifdef OS_AIX
  1895. SHM_LGPAGE | SHM_PIN |
  1896. #endif
  1897. IPC_CREAT | SHM_R | SHM_W);
  1898. if (shmid != -1) {
  1899. map_address = (void *)shmat(shmid, address, SHM_RND);
  1900. #ifdef OS_LINUX
  1901. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1902. #endif
  1903. if (map_address != (void *)-1){
  1904. shmctl(shmid, IPC_RMID, 0);
  1905. }
  1906. }
  1907. #endif
  1908. #ifdef __sun__
  1909. struct memcntl_mha mha;
  1910. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  1911. mha.mha_flags = 0;
  1912. mha.mha_pagesize = HUGE_PAGESIZE;
  1913. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  1914. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  1915. #endif
  1916. #ifdef OS_WINDOWS
  1917. HANDLE hToken;
  1918. TOKEN_PRIVILEGES tp;
  1919. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  1920. tp.PrivilegeCount = 1;
  1921. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1922. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  1923. CloseHandle(hToken);
  1924. return (void*)-1;
  1925. }
  1926. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  1927. CloseHandle(hToken);
  1928. return (void*)-1;
  1929. }
  1930. map_address = (void *)VirtualAlloc(address,
  1931. BUFFER_SIZE,
  1932. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  1933. PAGE_READWRITE);
  1934. tp.Privileges[0].Attributes = 0;
  1935. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  1936. if (map_address == (void *)NULL) map_address = (void *)-1;
  1937. #endif
  1938. if (map_address != (void *)-1){
  1939. release_info[release_pos].address = map_address;
  1940. release_info[release_pos].func = alloc_hugetlb_free;
  1941. release_pos ++;
  1942. }
  1943. return map_address;
  1944. }
  1945. #endif
  1946. #endif
  1947. #ifdef ALLOC_HUGETLBFILE
  1948. static int hugetlb_pid = 0;
  1949. static void alloc_hugetlbfile_free(struct release_t *release){
  1950. if (munmap(release -> address, BUFFER_SIZE)) {
  1951. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  1952. }
  1953. if (close(release -> attr)) {
  1954. printf("OpenBLAS : HugeTLBfs close failed.\n");
  1955. }
  1956. }
  1957. static void *alloc_hugetlbfile(void *address){
  1958. void *map_address = (void *)-1;
  1959. int fd;
  1960. char filename[64];
  1961. if (!hugetlb_pid) hugetlb_pid = getpid();
  1962. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  1963. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  1964. return (void *)-1;
  1965. }
  1966. unlink(filename);
  1967. map_address = mmap(address, BUFFER_SIZE,
  1968. PROT_READ | PROT_WRITE,
  1969. MAP_SHARED,
  1970. fd, 0);
  1971. if (map_address != (void *)-1) {
  1972. release_info[release_pos].address = map_address;
  1973. release_info[release_pos].attr = fd;
  1974. release_info[release_pos].func = alloc_hugetlbfile_free;
  1975. release_pos ++;
  1976. }
  1977. return map_address;
  1978. }
  1979. #endif
  1980. #ifdef SEEK_ADDRESS
  1981. static BLASULONG base_address = 0UL;
  1982. #else
  1983. static BLASULONG base_address = BASE_ADDRESS;
  1984. #endif
  1985. static volatile struct {
  1986. BLASULONG lock;
  1987. void *addr;
  1988. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  1989. int pos;
  1990. #endif
  1991. int used;
  1992. #ifndef __64BIT__
  1993. char dummy[48];
  1994. #else
  1995. char dummy[40];
  1996. #endif
  1997. } memory[NUM_BUFFERS];
  1998. static int memory_initialized = 0;
  1999. /* Memory allocation routine */
  2000. /* procpos ... indicates where it comes from */
  2001. /* 0 : Level 3 functions */
  2002. /* 1 : Level 2 functions */
  2003. /* 2 : Thread */
  2004. void *blas_memory_alloc(int procpos){
  2005. int position;
  2006. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2007. int mypos = 0;
  2008. #endif
  2009. void *map_address;
  2010. void *(*memoryalloc[])(void *address) = {
  2011. #ifdef ALLOC_DEVICEDRIVER
  2012. alloc_devicedirver,
  2013. #endif
  2014. /* Hugetlb implicitly assumes ALLOC_SHM */
  2015. #ifdef ALLOC_SHM
  2016. alloc_shm,
  2017. #endif
  2018. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  2019. alloc_hugetlb,
  2020. #endif
  2021. #ifdef ALLOC_MMAP
  2022. alloc_mmap,
  2023. #endif
  2024. #ifdef ALLOC_QALLOC
  2025. alloc_qalloc,
  2026. #endif
  2027. #ifdef ALLOC_WINDOWS
  2028. alloc_windows,
  2029. #endif
  2030. #ifdef ALLOC_MALLOC
  2031. alloc_malloc,
  2032. #endif
  2033. NULL,
  2034. };
  2035. void *(**func)(void *address);
  2036. #if defined(USE_OPENMP)
  2037. if (!memory_initialized) {
  2038. #endif
  2039. LOCK_COMMAND(&alloc_lock);
  2040. if (!memory_initialized) {
  2041. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2042. for (position = 0; position < NUM_BUFFERS; position ++){
  2043. memory[position].addr = (void *)0;
  2044. memory[position].pos = -1;
  2045. memory[position].used = 0;
  2046. memory[position].lock = 0;
  2047. }
  2048. #endif
  2049. #ifdef DYNAMIC_ARCH
  2050. gotoblas_dynamic_init();
  2051. #endif
  2052. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2053. gotoblas_affinity_init();
  2054. #endif
  2055. #ifdef SMP
  2056. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  2057. #endif
  2058. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  2059. #ifndef DYNAMIC_ARCH
  2060. blas_set_parameter();
  2061. #endif
  2062. #endif
  2063. memory_initialized = 1;
  2064. }
  2065. UNLOCK_COMMAND(&alloc_lock);
  2066. #if defined(USE_OPENMP)
  2067. }
  2068. #endif
  2069. #ifdef DEBUG
  2070. printf("Alloc Start ...\n");
  2071. #endif
  2072. /* #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2073. mypos = WhereAmI();
  2074. position = mypos;
  2075. while (position >= NUM_BUFFERS) position >>= 1;
  2076. do {
  2077. if (!memory[position].used && (memory[position].pos == mypos)) {
  2078. #if defined(SMP) && !defined(USE_OPENMP)
  2079. LOCK_COMMAND(&alloc_lock);
  2080. #else
  2081. blas_lock(&memory[position].lock);
  2082. #endif
  2083. if (!memory[position].used) goto allocation;
  2084. #if defined(SMP) && !defined(USE_OPENMP)
  2085. UNLOCK_COMMAND(&alloc_lock);
  2086. #else
  2087. blas_unlock(&memory[position].lock);
  2088. #endif
  2089. }
  2090. position ++;
  2091. } while (position < NUM_BUFFERS);
  2092. #endif */
  2093. position = 0;
  2094. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2095. LOCK_COMMAND(&alloc_lock);
  2096. #endif
  2097. do {
  2098. RMB;
  2099. #if defined(USE_OPENMP)
  2100. if (!memory[position].used) {
  2101. blas_lock(&memory[position].lock);
  2102. #endif
  2103. if (!memory[position].used) goto allocation;
  2104. #if defined(USE_OPENMP)
  2105. blas_unlock(&memory[position].lock);
  2106. }
  2107. #endif
  2108. position ++;
  2109. } while (position < NUM_BUFFERS);
  2110. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2111. UNLOCK_COMMAND(&alloc_lock);
  2112. #endif
  2113. goto error;
  2114. allocation :
  2115. #ifdef DEBUG
  2116. printf(" Position -> %d\n", position);
  2117. #endif
  2118. memory[position].used = 1;
  2119. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2120. UNLOCK_COMMAND(&alloc_lock);
  2121. #else
  2122. blas_unlock(&memory[position].lock);
  2123. #endif
  2124. if (!memory[position].addr) {
  2125. do {
  2126. #ifdef DEBUG
  2127. printf("Allocation Start : %lx\n", base_address);
  2128. #endif
  2129. map_address = (void *)-1;
  2130. func = &memoryalloc[0];
  2131. while ((func != NULL) && (map_address == (void *) -1)) {
  2132. map_address = (*func)((void *)base_address);
  2133. #ifdef ALLOC_DEVICEDRIVER
  2134. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  2135. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation was failed.\n");
  2136. }
  2137. #endif
  2138. #ifdef ALLOC_HUGETLBFILE
  2139. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  2140. #ifndef OS_WINDOWS
  2141. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  2142. #endif
  2143. }
  2144. #endif
  2145. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  2146. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  2147. #endif
  2148. func ++;
  2149. }
  2150. #ifdef DEBUG
  2151. printf(" Success -> %08lx\n", map_address);
  2152. #endif
  2153. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  2154. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  2155. } while ((BLASLONG)map_address == -1);
  2156. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2157. LOCK_COMMAND(&alloc_lock);
  2158. #endif
  2159. memory[position].addr = map_address;
  2160. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2161. UNLOCK_COMMAND(&alloc_lock);
  2162. #endif
  2163. #ifdef DEBUG
  2164. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  2165. #endif
  2166. }
  2167. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2168. if (memory[position].pos == -1) memory[position].pos = mypos;
  2169. #endif
  2170. #ifdef DYNAMIC_ARCH
  2171. if (memory_initialized == 1) {
  2172. LOCK_COMMAND(&alloc_lock);
  2173. if (memory_initialized == 1) {
  2174. if (!gotoblas) gotoblas_dynamic_init();
  2175. memory_initialized = 2;
  2176. }
  2177. UNLOCK_COMMAND(&alloc_lock);
  2178. }
  2179. #endif
  2180. #ifdef DEBUG
  2181. printf("Mapped : %p %3d\n\n",
  2182. (void *)memory[position].addr, position);
  2183. #endif
  2184. return (void *)memory[position].addr;
  2185. error:
  2186. printf("BLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  2187. return NULL;
  2188. }
  2189. void blas_memory_free(void *free_area){
  2190. int position;
  2191. #ifdef DEBUG
  2192. printf("Unmapped Start : %p ...\n", free_area);
  2193. #endif
  2194. position = 0;
  2195. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2196. LOCK_COMMAND(&alloc_lock);
  2197. #endif
  2198. while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
  2199. position++;
  2200. if (position >= NUM_BUFFERS) goto error;
  2201. #ifdef DEBUG
  2202. if (memory[position].addr != free_area) goto error;
  2203. printf(" Position : %d\n", position);
  2204. #endif
  2205. // arm: ensure all writes are finished before other thread takes this memory
  2206. WMB;
  2207. memory[position].used = 0;
  2208. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2209. UNLOCK_COMMAND(&alloc_lock);
  2210. #endif
  2211. #ifdef DEBUG
  2212. printf("Unmap Succeeded.\n\n");
  2213. #endif
  2214. return;
  2215. error:
  2216. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  2217. #ifdef DEBUG
  2218. for (position = 0; position < NUM_BUFFERS; position++)
  2219. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  2220. #endif
  2221. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2222. UNLOCK_COMMAND(&alloc_lock);
  2223. #endif
  2224. return;
  2225. }
  2226. void *blas_memory_alloc_nolock(int unused) {
  2227. void *map_address;
  2228. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  2229. return map_address;
  2230. }
  2231. void blas_memory_free_nolock(void * map_address) {
  2232. free(map_address);
  2233. }
  2234. void blas_shutdown(void){
  2235. int pos;
  2236. #ifdef SMP
  2237. BLASFUNC(blas_thread_shutdown)();
  2238. #endif
  2239. LOCK_COMMAND(&alloc_lock);
  2240. for (pos = 0; pos < release_pos; pos ++) {
  2241. release_info[pos].func(&release_info[pos]);
  2242. }
  2243. #ifdef SEEK_ADDRESS
  2244. base_address = 0UL;
  2245. #else
  2246. base_address = BASE_ADDRESS;
  2247. #endif
  2248. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  2249. memory[pos].addr = (void *)0;
  2250. memory[pos].used = 0;
  2251. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2252. memory[pos].pos = -1;
  2253. #endif
  2254. memory[pos].lock = 0;
  2255. }
  2256. UNLOCK_COMMAND(&alloc_lock);
  2257. return;
  2258. }
  2259. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2260. #if defined(SMP) || defined(USE_LOCKING)
  2261. #if defined(USE_PTHREAD_LOCK)
  2262. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  2263. #elif defined(USE_PTHREAD_SPINLOCK)
  2264. static pthread_spinlock_t init_lock = 0;
  2265. #else
  2266. static BLASULONG init_lock = 0UL;
  2267. #endif
  2268. #endif
  2269. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  2270. void *sa, void *sb, BLASLONG pos) {
  2271. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  2272. size_t size;
  2273. BLASULONG buffer;
  2274. size = BUFFER_SIZE - PAGESIZE;
  2275. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2276. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2277. if (hot_alloc != 2) {
  2278. #endif
  2279. #if defined(SMP) || defined(USE_LOCKING)
  2280. LOCK_COMMAND(&init_lock);
  2281. #endif
  2282. while (size > 0) {
  2283. *(int *)buffer = size;
  2284. buffer += PAGESIZE;
  2285. size -= PAGESIZE;
  2286. }
  2287. #if defined(SMP) || defined(USE_LOCKING)
  2288. UNLOCK_COMMAND(&init_lock);
  2289. #endif
  2290. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  2291. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2292. while (size > 0) {
  2293. *(int *)buffer = size;
  2294. buffer += 64;
  2295. size -= 64;
  2296. }
  2297. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2298. }
  2299. #endif
  2300. #endif
  2301. }
  2302. #ifdef SMP
  2303. static void _init_thread_memory(void *buffer) {
  2304. blas_queue_t queue[MAX_CPU_NUMBER];
  2305. int num_cpu;
  2306. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  2307. blas_queue_init(&queue[num_cpu]);
  2308. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  2309. queue[num_cpu].routine = &_touch_memory;
  2310. queue[num_cpu].args = NULL;
  2311. queue[num_cpu].next = &queue[num_cpu + 1];
  2312. }
  2313. queue[num_cpu - 1].next = NULL;
  2314. queue[0].sa = buffer;
  2315. exec_blas(num_cpu, queue);
  2316. }
  2317. #endif
  2318. static void gotoblas_memory_init(void) {
  2319. void *buffer;
  2320. hot_alloc = 1;
  2321. buffer = (void *)blas_memory_alloc(0);
  2322. #ifdef SMP
  2323. if (blas_cpu_number == 0) blas_get_cpu_number();
  2324. #ifdef SMP_SERVER
  2325. if (blas_server_avail == 0) blas_thread_init();
  2326. #endif
  2327. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  2328. #else
  2329. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  2330. #endif
  2331. blas_memory_free(buffer);
  2332. }
  2333. #endif
  2334. /* Initialization for all function; this function should be called before main */
  2335. static int gotoblas_initialized = 0;
  2336. extern void openblas_read_env();
  2337. void CONSTRUCTOR gotoblas_init(void) {
  2338. if (gotoblas_initialized) return;
  2339. #ifdef SMP
  2340. openblas_fork_handler();
  2341. #endif
  2342. openblas_read_env();
  2343. #ifdef PROFILE
  2344. moncontrol (0);
  2345. #endif
  2346. #ifdef DYNAMIC_ARCH
  2347. gotoblas_dynamic_init();
  2348. #endif
  2349. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2350. gotoblas_affinity_init();
  2351. #endif
  2352. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2353. gotoblas_memory_init();
  2354. #endif
  2355. //#if defined(OS_LINUX)
  2356. #if 0
  2357. struct rlimit curlimit;
  2358. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  2359. {
  2360. if ( curlimit.rlim_cur != curlimit.rlim_max )
  2361. {
  2362. curlimit.rlim_cur = curlimit.rlim_max;
  2363. setrlimit(RLIMIT_STACK, &curlimit);
  2364. }
  2365. }
  2366. #endif
  2367. #ifdef SMP
  2368. if (blas_cpu_number == 0) blas_get_cpu_number();
  2369. #ifdef SMP_SERVER
  2370. if (blas_server_avail == 0) blas_thread_init();
  2371. #endif
  2372. #endif
  2373. #ifdef FUNCTION_PROFILE
  2374. gotoblas_profile_init();
  2375. #endif
  2376. gotoblas_initialized = 1;
  2377. #ifdef PROFILE
  2378. moncontrol (1);
  2379. #endif
  2380. }
  2381. void DESTRUCTOR gotoblas_quit(void) {
  2382. if (gotoblas_initialized == 0) return;
  2383. blas_shutdown();
  2384. #ifdef PROFILE
  2385. moncontrol (0);
  2386. #endif
  2387. #ifdef FUNCTION_PROFILE
  2388. gotoblas_profile_quit();
  2389. #endif
  2390. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2391. gotoblas_affinity_quit();
  2392. #endif
  2393. #ifdef DYNAMIC_ARCH
  2394. gotoblas_dynamic_quit();
  2395. #endif
  2396. gotoblas_initialized = 0;
  2397. #ifdef PROFILE
  2398. moncontrol (1);
  2399. #endif
  2400. }
  2401. #if defined(_MSC_VER) && !defined(__clang__)
  2402. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  2403. {
  2404. switch (ul_reason_for_call)
  2405. {
  2406. case DLL_PROCESS_ATTACH:
  2407. gotoblas_init();
  2408. break;
  2409. case DLL_THREAD_ATTACH:
  2410. break;
  2411. case DLL_THREAD_DETACH:
  2412. break;
  2413. case DLL_PROCESS_DETACH:
  2414. gotoblas_quit();
  2415. break;
  2416. default:
  2417. break;
  2418. }
  2419. return TRUE;
  2420. }
  2421. /*
  2422. This is to allow static linking.
  2423. Code adapted from Google performance tools:
  2424. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  2425. Reference:
  2426. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  2427. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  2428. */
  2429. static int on_process_term(void)
  2430. {
  2431. gotoblas_quit();
  2432. return 0;
  2433. }
  2434. #ifdef _WIN64
  2435. #pragma comment(linker, "/INCLUDE:_tls_used")
  2436. #else
  2437. #pragma comment(linker, "/INCLUDE:__tls_used")
  2438. #endif
  2439. #ifdef _WIN64
  2440. #pragma const_seg(".CRT$XLB")
  2441. #else
  2442. #pragma data_seg(".CRT$XLB")
  2443. #endif
  2444. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  2445. #ifdef _WIN64
  2446. #pragma const_seg()
  2447. #else
  2448. #pragma data_seg()
  2449. #endif
  2450. #ifdef _WIN64
  2451. #pragma const_seg(".CRT$XTU")
  2452. #else
  2453. #pragma data_seg(".CRT$XTU")
  2454. #endif
  2455. static int(*p_process_term)(void) = on_process_term;
  2456. #ifdef _WIN64
  2457. #pragma const_seg()
  2458. #else
  2459. #pragma data_seg()
  2460. #endif
  2461. #endif
  2462. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  2463. /* Don't call me; this is just work around for PGI / Sun bug */
  2464. void gotoblas_dummy_for_PGI(void) {
  2465. gotoblas_init();
  2466. gotoblas_quit();
  2467. #if __PGIC__ < 19
  2468. #if 0
  2469. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  2470. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  2471. #else
  2472. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  2473. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  2474. #endif
  2475. #endif
  2476. }
  2477. #endif
  2478. #endif