# How can I prevent the memory distributed in Julia?

**URL:** <https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426>\
**Category:** Performance\
**Tags:** parallel\
**Created:** [September 19, 2021, 11:41pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426 "2021-09-19T23:41:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [September 19, 2021, 11:41pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/1 "2021-09-19T23:41:07Z")

</div>

I want to use julia to write a code parallel reading data from txt file and saved into a big matrix.  
However, I found the matrix seems to be distributed to each CPU and I need each CPU to be tremedous big. How can I prevent the matrix to be distributed?

This is the code

```julia
using DelimitedFiles
using LinearAlgebra
using Statistics
using JLD
N = 2120;
ins_force = zeros(N,3,5000);
Force = zeros(N,3,5000,3);
Threads.@threads for i = 1:3
    start = time()
    file = readdlm(string("Force",5000*(i-1),".txt"))
    index = 1;
    idx = 1;
    while idx < size(file,1)
        ins_force[:,:,index] = file[idx+1:idx+N,:];
        index = index+1;
        idx = idx +N+1;
    end
    time_end =time()
    Force[:,:,:,i] = ins_force;
    display(time_end-start)
end

```

I can use the cell and cell2mat to realize this by code

```julia
clc
clear
%% compute ins force
N = 2120;
list = dir(['./Force*'])
n = size(list,1)
index = 1;
Force = cell(1,1,200);
parfor i = 1:200
    tic
    A = textread(['./Force',num2str(5000*(i-1)),'.txt']);
    index = 1
    Force_temp= zeros(N,3,5000);
    idx = 1;
    while idx < size(A,1)
        Force_temp(:,:,index) = A(idx+1:idx+N,:);
        index = index+1;
        idx = idx +N+1;
    end
    Force{1,1,i} = Force_temp;
    toc
end
Force = cell2mat(Force);

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 19, 2021, 11:47pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/2 "2021-09-19T23:47:55Z")

</div>

please use triple ` to wrap your code:

````julia
```
like this
```

````

---

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [September 20, 2021, 12:00am UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/4 "2021-09-20T00:00:36Z")

</div>

Thank you for your suggestion

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 20, 2021, 7:45am UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/5 "2021-09-20T07:45:39Z")

</div>

Your question is a little unclear to me. Do you want your code to be multithreaded or not multithreaded? Regular Julia matrices are not distributed and all memory is visible to all CPUs when using threads.

---

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [September 20, 2021, 5:37pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/6 "2021-09-20T17:37:58Z")

</div>

I want the code to be multithreaded. The point is when the same code is running (Julia and Matlab do the almost similar things.). The Julia will Out of Memory. It seems to me that the Matrix is distributed? Maybe I have some misunderstanding for it. But is there other reasons to make the Julia code Out of Memory, for one or two prcessor?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [September 20, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/7 "2021-09-20T18:03:34Z")

</div>

> [@Lyupinpin](#):
>
> ```julia
> ins_force = zeros(N,3,5000);
> Force = zeros(N,3,5000,3);
> Threads.@threads for i = 1:3
> start = time()
> file = readdlm(string("Force",5000*(i-1),".txt"))
> index = 1;
> idx = 1;
> while idx < size(file,1)
> ins_force[:,:,index] = file[idx+1:idx+N,:];
> 
> ```

This code is not thread-safe: `ins_force` is defnied outside of the multithreaded part, but used inside without accounting for the thread-loop index `i`.  
Note that the Matlab version does not have this issue, there `Force_temp` is defined inside the `parfor` loop.  
Furthermore, this code should be wrapped in a function to avoid usage of global variables, which are type-unstable.

---

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [September 20, 2021, 6:18pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/8 "2021-09-20T18:18:20Z")

</div>

Thank you for your useful suggestions!  
Does this seems to be thread\_save now?  
I am a new parallel user. I’m not sure how to make it safe in Julia.

```julia
using DelimitedFiles
using LinearAlgebra
using Statistics
using JLD
N = 2120;
Force = zeros(N,3,5000,3);

function readtxt(i)
    ins_force = zeros(N,3,5000);
    file = readdlm(string("Force",5000*(i-1),".txt"))
    index = 1;
    idx = 1;
    start =time()
    while idx < size(file,1)
        ins_force[:,:,index] = file[idx+1:idx+N,:];
        index = index+1;
        idx = idx +N+1;
    end
    time_end =time()
    display(time_end-start)
    return ins_force
end
Threads.@threads for i = 1:3
    Force[:,:,:,i] = readtxt(i);
end

```

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [September 20, 2021, 6:20pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/9 "2021-09-20T18:20:31Z")

</div>

> [@Lyupinpin](#):
>
> `N = 2120;`

Yes. In addition you should define `N` either as const or as a function parameter to make your inner loop also type-stable.  
Maybe this already solves your memory issues?

---

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [September 20, 2021, 8:18pm UTC](https://discourse.julialang.org/t/how-can-i-prevent-the-memory-distributed-in-julia/68426/10 "2021-09-20T20:18:11Z")

</div>

OK. I will have a try!

Thanks a lot.
