Atomic Operations Example


This example is the same as the Collatz Conjecture example execept that it makes use of Atomic Operations, enabling multiple threads to modify a single piece of data at once.

This highlights an advanced use of the library which requires you to manually ensure that you do not operate on any elements you are also modifying on another thread, or you'll introduce race conditions.

When memory is shared, multiple threads can read and write the same data in memory. Atomic operations make sure that predictable values are written and read, that operations are finished before the next operation starts and that operations are not interrupted.

Learn more MDN Docs Atomics

Provided example will generate an array containing every number between the start and end values and calculate the number of steps taken before each number reaches the value of 1 by following the collatz conjecture pattern. When the benchmark is complete, it should render a graph showing how much faster (or slower) the logic ran with each additional thread.


                    
                      const operator = function() {
                        var sharedArray = params.sharedArray; // Use shared array directly
                        var start = params.index.start || 0;
                        var end = params.index.end !== undefined ? params.index.end : sharedArray.length - 1;
                       
                        for (var i = start; i <= end; i++) {
                          var x = Atomics.load(sharedArray, i); // Load the value atomically
                    
                          var steps = 0;
                    
                          while (x !== 1) {
                            if (x % 2 === 0) {
                              x = x / 2;
                            } else {
                              x = x * 3 + 1;
                            }
                            steps++;
                          }
                    
                          Atomics.store(sharedArray, i, steps); // Store the result atomically
                        }
                        // No need to return anything, shared array is modified in place without race conditions thanks to precalculated index's
                      };
                      const params = {
                        sharedArray: new Int32Array([12323,2232,33232,42112,50209,665,712,823,9292,1001]),
                        dataType: 'Int32'
                      };
                      hamsters.run(params, operator, function(results) {
                        console.info(results);
                      }, function(error) {
                        console.error(error);
                      });
                    
                

Client Summary

  • Hamsters.js Version:
  • Maximum Threads:
  • Browser:
  • Legacy Mode:
  • Atomic Operations Support:
  • Transferable Object Support:




Execution Time - Lower is better

Thread Scaling % Improvement - Higher is better