# Code organization for running on a cluster

**URL:** <https://discourse.julialang.org/t/code-organization-for-running-on-a-cluster/83215>\
**Category:** General Usage\
**Tags:** distributed\
**Created:** [June 23, 2022, 12:37am UTC](https://discourse.julialang.org/t/code-organization-for-running-on-a-cluster/83215 "2022-06-23T00:37:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [June 23, 2022, 12:37am UTC](https://discourse.julialang.org/t/code-organization-for-running-on-a-cluster/83215/1 "2022-06-23T00:37:51Z")

</div>

I’d like to get some input on how to organize my code to run on a computer cluster. I have programmed the computation I want to perform on a file `main.jl`. The contents of `main.jl` would be something like this:

```julia
#! main.jl

using Pkg
Pkg.activate(".")
Pkg.instantiate()

using Random
using Folds

Random.seed!(123)

const R = 5

simulate_data() = rand(100_000, 100)

function auxiliarymodel(data)
    xx = data[:, 2:end]
    y = data[:, 1]
    return xx\y
end

likelihood(x) = sum(x)

function func(R, params)
    function sdatamodel(i)
        sdata = simulate_data()
        return auxiliarymodel(sdata)
    end

    avgb = Folds.mapreduce(sdatamodel, +, 1:R, DistributedEx(); init=zero(params)) ./ R
    return likelihood(avgb)
end

func(50, ones(99)) |> println

```

I’m defining and calling functions in `main.jl`. Those functions are mostly serial, but the function `func` is costly, and that is the one I’d like to computing using multiple processors.

I also program another file `multiproc.jl` that calls `main.jl` and sets up the multiprocessor computation.

```julia
#! multiproc.jl

using Distributed
addprocs()

@everywhere include("main.jl")

```

My idea is to start julia and execute `multiproc.jl`:

```julia
$ julia -p 10 multiproc.jl

```

Is this a good organization of the code? and most importantly, would this achieve what I want?
