You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory.c 76 kB

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