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