# Implementation of a cellular automata to model breast cancer

I've created a program - it's an implementation of the cellular automata described in this paper (can't find a free PDF, sorry - anyway it's not really relevant to this question). The CA is implemented as a 3D array of integers of size int[length][20][20], 'length' being an arbitrary value determined by the user.

The tl;dr is: I've parallelized the program and not only does it not speed up, it slows down sometimes. Here's the code for the sequential and concurrent versions of the program, as well as a main benchmarking program (this and this).

The way it's parallelized is that the array is distributed between threads - say if length = 200 and we use 4 threads, thread 0 would process (we will call the lattice itself 'duct' from now on) duct[0][0][0] through duct[49][19][19], thread 1 would process duct[50][0][0] through duct[99][19][19], and so on. While processing a cell, a thread might have to access a different thread domain, so ReentrantLocks are used on the boundaries between sections to preserve integrity. Synchronization is achieved with a CyclicBarrier.

Anyway, the program is not speeding up at all. I suspect it's due to a memory bottleneck; while processing a given cell the program accesses its 26 neighbors, which might be the reason for the slow results. Otherwise, I've no idea where to start optimizing this program so parallelization yields results, or even if parallelization will ever yield results with this particular program.

Since I've to post some code here besides pastebin, here's how parallelization is implemented. This is done on the constructor:

int step = length/n_threads;
for(int i=0 ; i<n_threads ; i++)
{
int begin =     i*step;
int end   = (i+1)*step-1;
end = length-1;

threadArray[i] = new Duct(begin, end, i);
}
}


public Duct(int begin, int end, int index)
{
index_array = new Integer[end-begin+1];
// Aquí Coordinate se usa como un par de números, no como una coordenada
boundaries = new Coordinate(begin, end, 0);
// [begin, end] ambos inclusive
for(int z=begin, i=0 ; z<=end ; z++, i++)
{
index_array[i] = z;
}
}


and the function that controls synchronization

protected void execute(int nGens)
{
if(generation_number == 0)
{
init_routine_stem(); // Hacemos la primera generación (colocación de stem cells) secuencialmente
}

total_generations = nGens;

try {
} catch(Exception e){System.out.println(e);}

for (int i=0; i<total_generations; i++)
{
try
{
generation_number++;

if(!initialized)
{