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