JuliaCon 2023

KomaMRI.jl: Framework for MRI Simulations with GPU Acceleration
07-27, 11:00–11:30 (US/Eastern), 26-100

Koma is an MRI simulator utilizing CPU and GPU parallelization to solve the Bloch equations. Our simulator targets researchers and students, offering an easy-to-use GUI. The accuracy and speed of our simulator were compared against two open-source alternatives JEMRIS (C++, using an ODE solver) and MRiLab (C++ and CUDA). The results show that Koma can simulate with high accuracy (MAEs below 0.1% compared to JEMRIS) and better GPU performance than MRiLab.


Numerical simulations are an important tool for analyzing and developing new acquisition and reconstruction methods in Magnetic Resonance Imaging (MRI). Simulations allow us to isolate and study phenomena by removing unwanted effects, such as hardware imperfections, and others. Additionally, with the increasing use of Machine Learning models, simulation becomes even more relevant, because it can be used to generate synthetic data for training, or to construct signal dictionaries to infer quantitative measurements from the acquired data. Moreover, simulations are an excellent tool for education and training, as hands-on experience is a great way to assimilate the theoretical and practical components of MRI.

We believe an ideal simulator should be general, fast, easy to use, extensible, open-source, and cross-platform. In this work, we developed an MRI simulation framework built from the ground up to satisfy these requirements. We chose the Julia programming language because its syntax is similar to MATLAB (widely used by the MRI community), its excellent GPU support, and its speed is comparable to C/C++. This has been shown to be the case in other MRI applications such as image reconstruction with MRIReco.jl, where the authors achieved speeds on par with state-of-the-art toolboxes.

We called our simulator “Koma”, inspired by the Japanese word for spinning top, as its physics resemble MRI’s


Koma simulates the magnetization the spins in a virtual object by solving the Bloch equations. The solution of these equations for a single spin is independent of the state of the other spins in the system, a key feature that enables parallelization.

Our simulator uses a first-order splitting method to simplify the solution of the Bloch equations. This reflects mathematically the intuition of separating the Bloch equations in a two-step process, rotation and relaxation, for each time step. This method is exact in many common cases in MRI, but in general, has O(dt^3) convergence.

For CPU parallelization, we used the macro Threads.@threads, and the package ThreadsX.jl. To ensure thread safety, we stored the acquired signals per thread in different matrices to add them later into a signal matrix. On the other hand, for the GPU support we operated with CuArrays from CUDA.jl.

We ensured type stability to enable high performance. Moreover, we had special care to perform in-place operations and not generate unnecessary variable copies using the @view macro in the simulation functions. Finally, we used NVIDIA Nsight Systems to profile GPU performance with the NVTX.@range and CUDA.@profile macros.

For the GUI we used Blink.jl, a framework to develop applications using web technologies. The GUI allows the user to easily plot the sequence, 𝑘-space, phantom, acquired signal, and reconstructed images. Plots are done using the PlotlyJS.jl package, which also allows to export them to .svg files.

To test the accuracy of our simulator, we compared Koma with the latest version of JEMRIS (v2.9), which has been compared with real MRI acquisitions. On the other hand, we compared the speed of our simulations against MRiLab, an open-source GPU-accelerated MRI simulator. Finally, to compare the ease of use for first-time users, we designed a pilot experience with students of an Imaging course in Engineering, where they learned some fundamentals of MRI.


For the simulated scenarios we obtained accurate results with MAEs below 0.1% when compared to JEMRIS. When we tested the simulation speed against MRiLab, we found that we had slower CPU performance, but we were 2.6 times faster for the GTX 1650Ti and 6.0 times for the RTX 2080 Ti.

We think the CPU results show that we still perform unwanted synchronizations between threads, a problem that our GPU implementation would not suffer as we use Nthreads=1 by default.

In the students experience, they reported no problem installing Julia (mean 4.7/5), Koma (mean 4.2/5), JEMRIS (3.8/5), and MRiLab (4.3/5). Regarding the time taken to install each simulator, most of the students were able to install Koma (mean 13.2 min), JEMRIS (mean 33.8 min), and MRiLab (mean 16.9 min) in less than 40 minutes.

Their first simulation took them more time in JEMRIS (mean 19 min) and MRiLab (mean 13.9 min) than in Koma (mean 5.7 min). 31% of the students could not simulate on MRiLab (6 students using Mac OS), so we only used Koma and JEMRIS for the rest of the activities.

Their reported median simulation speeds were 8.4 times faster with Koma than with JEMRIS, and 65% recommended Koma over JEMRIS.


In this work, we presented a new general MRI simulator programmed in Julia. This simulator is fast, easy to use, extensible, open-source, and cross-platform. These characteristics were achieved by choosing the appropriate technologies to write easy-to-understand and fast code with a flexible GUI.

Carlos Castillo-Passi received a M.Sc degree in Electrical Engineering in 2018 from Pontificia Universidad Catolica de Chile. He is currently studying for a Ph.D. in Biomedical Engineering and Imaging Science Research, as a part of a joint degree between King's College London and Pontificia Universidad Catolica de Chile. His research interests include inverse problems, image reconstruction and formation, and MRI hardware.