title: How to modify elements of a vector based on other elements in parallel?
body: <p>I'm trying to parallelize part of my code, but I can't figure out how since everywhere I read about parallelization the object is completely split and then processed using only the "information" in the chunk. My problem cannot be split in independent chunks, but the updates can be done independently. </p>
  
  <p>Here's a simplified version</p>
  
  <pre><code>a &lt;- runif(10000)
  indexes &lt;- c(seq(1,9999,2), seq(2,10000,2))
  for(i in indexes){
      .prev &lt;- ifelse(i&gt;1, a[i-1], 0)
      .next &lt;- ifelse(i&lt;10000, a[i+1], 1)
      a[i] &lt;- runif(1, min(.prev,.next), max(.prev, .next))
  }
  </code></pre>
  
  <p>While each iteration on the for loop depends on the current values in <code>a</code>, the order defined in <code>indexes</code> makes the problem parallelized within even and odd indexes, e.g., if <code>indexes = seq(1,9999,2)</code> the dependence is not a problem since the values used in each iteration won't ever be modified. On the other hand it will be impossible to execute this with the subset <code>a[indexes]</code>, hence the "splitting" strategy in the guides I read are unable to perform this operation.</p>
  
  <p>How can I parallelize a problem like this, where I need the object to be modified and "look" at the whole vector every time? Instead of each worker returning some output, each worker has to modify a "shared" object in memory?</p>