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?
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)