using MPI
MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(MPI.COMM_WORLD)
size = MPI.Comm_size(MPI.COMM_WORLD)
buf = fill(0, 10)
win = MPI.MPI_Win
MPI.API.MPI_Win_allocate(10*sizeof(Int), sizeof(Int), MPI.INFO_NULL, comm, buf, win)
if rank == 0
    @views buf .= 1:10
else
    @views buf .= 1 
end
@show (rank, buf)
if rank !== 0
    MPI.Win_lock(MPI.LOCK_EXCLUSIVE, 0, 0, win)
    MPI.Get(buf, 10, MPI.Int, rank, sizeof(Int), 10, MPI.Int, win)
    MPI.Win_unlock(0, win)
end
@show (rank, buf)
MPI.free(win)
This is small program illustrating use of RMA (remote memory access) functionality of MPI. The syntax I am using for MPI.API.MPI_Win_allocate() is as given in MPI.jl docs:MPI_Win_allocate(size, disp_unit, info, comm, baseptr, win)
(base) $ mpiexecjl -np 2 julia test_rma.jl 
ERROR: ERROR: LoadError: LoadError: MethodError: no method matching unsafe_convert(::Type{Ptr{Int32}}, ::Type{Int32})MethodError: no method matching unsafe_convert(::Type{Ptr{Int32}}, ::Type{Int32})
Closest candidates are:
  unsafe_convert(::Type{Ptr{Int32}}, !Matched::MPI.Group)
   @ MPI ~/.julia/packages/MPI/z2owj/src/group.jl:12
  unsafe_convert(::Type{Ptr{Int32}}, !Matched::MPI.Errhandler)
   @ MPI ~/.julia/packages/MPI/z2owj/src/errhandler.jl:15
  unsafe_convert(::Type{Ptr{Int32}}, !Matched::MPI.Request)
   @ MPI ~/.julia/packages/MPI/z2owj/src/nonblocking.jl:190
  ...
Can anyone tell me why there is a error related to unsafe wrap?
             
            
              
            
           
          
            
            
              
 Devansh1106:
 
MPI_Win
 
 
Don’t you want to do something like
win = MPI.Win_create(all_ranks, MPI.COMM_WORLD)
?
             
            
              
            
           
          
            
            
              Yeah, that is one method. But I want to use MPI.API.MPI_Win_allocate() instead of MPI.Win_create().
             
            
              
            
           
          
            
            
              The last argument is a pointer to a storage location for the output:
r_win = Ref{MPI_Win}()
status = ...., r_win)
win = r_win[]
Or something similar. Check how Win_create works
             
            
              
            
           
          
            
            
              so r_win is a reference to a window object?baseptr ?
Where can I check about Win_create’s working? I could not find it in the docs.
             
            
              
            
           
          
            
            
              Looking at MPI_Win_allocate  the baseptr needs to be handled similarly.
I would also recommend reading Calling C and Fortran Code · The Julia Language 
Probably best to look at MPI.jl/src/onesided.jl at 7b97da711b90f2c0769ffe513a946958b4ec8ae1 · JuliaParallel/MPI.jl · GitHub 
Turns out our Win object is a bit more magical, since it is GC tracked so that when it goes out of scope we deallocate the window.
I would recommend opening a feature PR to MPI.jl adding Win_allocate
             
            
              
            
           
          
            
            
              Thanks for the resources.Win_allocate is available though using api in the file MPI.jl/src/api/generated_api.jl.
             
            
              
            
           
          
            
            
              what does this line status = ...., r_win) means?MethodError: Cannot convert an object of type Int64 to an object of type MPI.API.MPIPtr
I have defined baseptr as:
r_buf = Ref{Int64}()
buf = r_buf[] 
 
            
              
            
           
          
            
            
              The raw underlying C API is available that way, in order to use it you need to know how to interact with C from Julia.
MPI.jl provides high-level functions to make interacting with the C level convenient and less error prone.
             
            
              
            
           
          
          
            
            
              I think MPI.MPI_Win is a datatype .
             
            
              
            
           
          
            
            
              yeah, somehow related to Int32