What are shared arrays in Julia?

Overview

Julia is an open-source language that is dynamic, fast, and user-friendly.

A shared array uses memory that has been shared with the system to map the same array across many processes. In SharedArray, each process has access to the entire array.

A shared array is beneficial when we have to make large amounts of data jointly accessible to multiple processes on the same machine.

Differences between a shared array and a distributed array

Shared array

  • The process has access to the entire array of data. The user can access all data in the array without any limitations.
  • Multiple processes can have access to the same data.
  • For each of the CPUs, the same array is copied multiple times.

Distributed array

  • The process has local access to only the data that is assigned to it. The user has access to their limited share of the data.
  • Two different processes do not have access to the same data.
  • There is only one array, and each CPU has access to only a portion of it.

A visual presentation

Each CPU has access to only its own array segment
1 of 2

Syntax

The syntax to create a shared array is as follows.

Without the init function

SharedArray{T}(dims::NTuple; init=false, pids=Int[])
SharedArray{T,N}(...)
  • T: This is the bits type.
  • dims: This is the size.
  • pids: These specify these processes. The processes are on the same host.
  • N: The value for this has to be the same as dims.

Note: When pids is not provided, the shared array is mapped across all processes, including the master process.

With the init function

SharedArray{T}(file::AbstractString, dims::NTuple, [offset=0]; mode=nothing, init=false, pids=Int[])
SharedArray{T,N}(...)

In this scenario, the shared array is backed by the file with the filename, with size dims, element type T (must be a bits type), processes that are specified by pids, and all the processes must be on the same host. The file is going to be mapped into the host memory with the following implications:

  1. The array data is to be in binary format.

  2. If a change is made to the array values, the values on the disk would also change.

The mode can only be one of r, r+, w+, and a+. If the filename is not specified, the mode will be w+, otherwise by default it is r+.

The offset permits us to skip a given number of bytes at the beginning of the file.

Note: An init function cannot be specified if the file is unwritable.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved