# Crooked array saving to files

**URL:** <https://discourse.julialang.org/t/crooked-array-saving-to-files/99855>\
**Category:** General Usage\
**Created:** [June 4, 2023, 5:48pm UTC](https://discourse.julialang.org/t/crooked-array-saving-to-files/99855 "2023-06-04T17:48:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![k.kirshin123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k.kirshin123/32/50436_2.png) [@k.kirshin123](https://discourse.julialang.org/u/k.kirshin123)\
**Post date:** [June 4, 2023, 5:48pm UTC](https://discourse.julialang.org/t/crooked-array-saving-to-files/99855/1 "2023-06-04T17:48:10Z")

</div>

Hello everyone, I need to write two random matrices to txt files and read them later, but every time I try to do this, I notice that the files were written very crookedly, my program does not work from this.  
As I have read, it is not possible to write a two-dimensional array (matrix) to a txt file, so the question is how to do it right?

```julia
using DelimitedFiles
using LinearAlgebra
using MPI
MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
size = MPI.Comm_size(comm)
N = 100
function create_random_matrices()
    A = rand(Float64, N, N)
    B = rand(Float64, N, N)
    writedlm("matrix_A.csv", A, ',')
    writedlm("matrix_B.csv", B, ',')
end

function multiply_matrices()
    MPI.Init()

    comm = MPI.COMM_WORLD
    rank = MPI.Comm_rank(comm)
    size = MPI.Comm_size(comm)

    if rank == 0
        A = readdlm("matrix_A.csv", ',', Float64)
        B = readdlm("matrix_B.csv", ',', Float64) 
        start_time = MPI.Wtime()
    else
        A = zeros(N, N)
        B = zeros(N, N)
    end

    block_size = Int64(N // size)
    local_A = zeros(block_size, N)
    local_B = zeros(N, block_size)
    local_C = zeros(block_size, block_size)
    MPI.Scatter!(A, local_A, comm)
    MPI.Scatter!(B, local_B, comm)

    for i in 1:block_size
        for j in 1:block_size
            for k in 1:block_size
                local_C[i, j] += dot(local_A[i, k], local_B[k, j])
            end
        end
    end

    C = MPI.Gather(local_C, comm)
   if rank == 0
       elapse_time=MPI.Wtime()-start_time
       return(elapse_time)
    end

    MPI.Finalize()
end
create_random_matrices()
elapse_time = multiply_matrices()
println(elapse_time)

```

 ![gPtDuUhz4CY](https://global.discourse-cdn.com/julialang/original/3X/a/9/a97266447a8aaebaf0a7e519f63f6d8948be15a3.jpeg)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 4, 2023, 6:48pm UTC](https://discourse.julialang.org/t/crooked-array-saving-to-files/99855/2 "2023-06-04T18:48:44Z")

</div>

Hi @k.kirshin123, what do you mean by “crookedly”? Are you referring to the missing spaces / line breaks between the rows of your matrix in the text file?  
I just tried `create_random_matrices()` and it worked properly on my laptop. Maybe your text editor autoformatted the CSVs?

> [@k.kirshin123](#):
>
> As I have read, it is not possible to write a two-dimensional array (matrix) to a txt file, so the question is how to do it right?

Where did you read this?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [June 4, 2023, 6:59pm UTC](https://discourse.julialang.org/t/crooked-array-saving-to-files/99855/3 "2023-06-04T18:59:05Z")

</div>

~~I think the issue is that you are calling `MPI.Init()` at the start of the script, and not inside the `multiply_matrices` function, like you did here [Help me pls with method error](https://discourse.julialang.org/t/help-me-pls-with-method-error/99763)  
Hence, multiple processors are trying to write to the same file at the same time.~~  
I think this happens regardless of whether you have called `MPI.Init()`.

~~Either move `MPI.Init()` inside the function or~~ Do something like this to only create the matrices once:

```julia
MPI.Init()
if rank == 0
   create_random_matrices()
end
MPI.Barrier()

```
