Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix multiplication not thread safe. #22581

Closed
xivaxy opened this issue Jun 27, 2017 · 4 comments · Fixed by #26562
Closed

Matrix multiplication not thread safe. #22581

xivaxy opened this issue Jun 27, 2017 · 4 comments · Fixed by #26562
Labels
linear algebra Linear algebra multithreading Base.Threads and related functionality

Comments

@xivaxy
Copy link

xivaxy commented Jun 27, 2017

MRE: several copies of a matrix are multiplied by the same matrix several times independently. The result should be the same for all copies, but the function usually returns false for N>=4. Some multiplication are not performed correctly.

function testMM(T, R, N)
    m0 = rand(0:2, (N,N))
    m1 = rand(0:2, (N,N))
    m = [deepcopy(m0) for i=1:R]
    Threads.@threads for i=1:R
        for t=1:T
            m[i] *= m1
        end
    end
    return all(x->(x==m[1]),m)
end 

testMM(5,8,3)
# true

testMM(5,8,4)
# false - most likely

Threads.nthreads() is 4 for me. With a normal For-loop without threads the function always returns true.

The issue only happens with matrix multiplication, but not with addition, multiplying by a scalar or element-wise multiplication.

Julia Version 0.6.0
Commit 903644385b* (2017-06-19 13:05 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, ivybridge)
@andreasnoack
Copy link
Member

andreasnoack commented Jun 27, 2017

I'm unable to reproduce this with

Julia Version 0.6.0-rc2.0
Commit 68e911be53 (2017-05-18 02:31 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

and

Julia Version 0.7.0-DEV.653
Commit 32aa9edad4* (2017-06-19 17:55 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin16.7.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT NO_AFFINITY HASWELL)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)
Environment:

@yuyichao
Copy link
Contributor

And you did set the thread count?

@andreasnoack
Copy link
Member

Oops. Typo in environment variable. Updated.

@yuyichao
Copy link
Contributor

There are at least three global buffers in matmul.jl that's not thread safe.

@yuyichao yuyichao added linear algebra Linear algebra multithreading Base.Threads and related functionality labels Jun 27, 2017
@yuyichao yuyichao changed the title Matrix multiplication in Threads.@threads-ForLoop returns incorrect results Matrix multiplication not thread safe. Jun 27, 2017
stevengj added a commit to stevengj/julia that referenced this issue Mar 21, 2018
stevengj added a commit that referenced this issue Mar 28, 2018
* make generic matmul thread-safe (fixes #22581)

* use per-thread DIGITS buffer in grisu and printf (fixes #25727)

* refactor resize_nthreads into an unexported Threads function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra multithreading Base.Threads and related functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants