dsb-lab / CellBasedModels.jl

Julia package for multicellular modeling
MIT License
19 stars 3 forks source link

GPU acceleration on the Patterning example #33

Closed dirypan closed 1 year ago

dirypan commented 1 year ago

I followed the Patterning example in the documentation and successfully run it. Then I tried to use the GPU acceleration by claiming platform in the community initialization and not changing anything else: com = Community(model, N=2*Nx*Ny, dt=0.001, agentAlg=DifferentialEquations.EM(), platform=GPU() ) This is the error I got when running evolve!(com,steps=4000,saveEach=10,saveCurrentState=true); InvalidIRError: compiling MethodInstance for (::var"#kernel#5")(::CuDeviceMatrix{Float32, 1}, ::CuDeviceMatrix{Float32, 1}, ::Float64, ::Float64, ::Int64, ::CuDeviceVector{Int64, 1}, ::Int64, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Int64, 1}, ::CuDeviceMatrix{Float32, 1}, ::Float64, ::Float64, ::Float64, ::CuDeviceVector{Int64, 1}, ::CuDeviceVector{Int64, 1}, ::CuDeviceVector{Int64, 1}, ::CuDeviceVector{Int64, 1}, ::CuDeviceVector{Int64, 1}, ::CuDeviceVector{Int64, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}, ::CuDeviceVector{Float32, 1}) resulted in invalid LLVM IR Reason: unsupported call through a literal pointer (call to ijl_alloc_array_1d) Stacktrace: [1] Array @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)boot.jl:477 [2] Array @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)boot.jl:486 [3] similar @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)abstractarray.jl:884 [4] similar @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)abstractarray.jl:883 [5] _array_for @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)array.jl:671 [6] _array_for @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)array.jl:674 [7] vect @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)array.jl:126 [8] macro expansion @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)In[3]:26 [9] macro expansion @ [~/.julia/packages/CellBasedModels/InCqt/src/](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/~/.julia/packages/CellBasedModels/InCqt/src/)neighbors.jl:38 [10] macro expansion @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)In[3]:25 [11] macro expansion @ [~/.julia/packages/CellBasedModels/InCqt/src/AgentStructure/](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/~/.julia/packages/CellBasedModels/InCqt/src/AgentStructure/)auxiliar.jl:183 [12] kernel @ [~/.julia/packages/CellBasedModels/InCqt/src/AgentStructure/](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/~/.julia/packages/CellBasedModels/InCqt/src/AgentStructure/)functionDE.jl:183 Reason: unsupported dynamic function invocation (call to mapreduce_empty_iter(f, op, itr, ItrEltype) @ Base reduce.jl:375) Stacktrace: [1] _mapreduce @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)reduce.jl:427 [2] _mapreduce_dim @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)reducedim.jl:365 [3] #mapreduce#800 @ [./](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/)reducedim.jl:357 ... @ CellBasedModels [~/.julia/packages/CellBasedModels/InCqt/src/CommunityStructure/step.jl:99](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/~/.julia/packages/CellBasedModels/InCqt/src/CommunityStructure/step.jl:99) [29] evolve!(community::Community; steps::Int64, saveEach::Int64, saveToFile::Bool, fileName::Nothing, overwrite::Bool, saveCurrentState::Bool, preallocateAgents::Int64, progressMessage::CellBasedModels.var"#291#293") @ CellBasedModels [~/.julia/packages/CellBasedModels/InCqt/src/CommunityStructure/step.jl:144](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/dirypan/Project/CellBasedModel/~/.julia/packages/CellBasedModels/InCqt/src/CommunityStructure/step.jl:144) [30] top-level scope @ In[5]:1

Just to show that it might not be a CUDA problem since I can run the Bacterial Colony Growth example with GPU acceleration without any problem.

I would appreciate that if you could point out where the problem could be. I want to apply this package to my study on agent based reaction-diffusion project.

dirypan commented 1 year ago

I figured out that the minimum function in julia is not CUDA compatible, I rewrite that into the following code to calculate minimum d: original code: d = minimum( [ (x-x[i2])^2+(y-y[i2])^2, (x-x[i2]+1)^2+(y-y[i2])^2, (x-x[i2]-1)^2+(y-y[i2])^2, (x-x[i2])^2+(y-y[i2]+1)^2, (x-x[i2])^2+(y-y[i2]-1)^2, (x-x[i2]+1)^2+(y-y[i2]+1)^2, (x-x[i2]+1)^2+(y-y[i2]-1)^2, (x-x[i2]-1)^2+(y-y[i2]+1)^2, (x-x[i2]-1)^2+(y-y[i2]-1)^2 ] ) modified: d = (x-x[i2])^2+(y-y[i2])^2 for ix in -1:1 for iy in -1:1 d_ = (x-x[i2]+ix)^2+(y-y[i2]+iy)^2 if d_ < d d = d_ end end end Now it works perfectly, I also compared the speed on my computer with CPU(i7 13700k) and RTX 3090ti, it is about 4 times faster on the GPU.