1- #include < faasm/counter.h>
21#include < faasm/faasm.h>
32#include < faasm/input.h>
43#include < faasm/random.h>
1211
1312int piStep ()
1413{
15- auto sum = faasm::AtomicInt (" pi" );
16-
1714 int count = 0 ;
1815 for (int i = 0 ; i < CHUNK_SIZE; i++) {
1916 // Two random points
@@ -26,8 +23,8 @@ int piStep()
2623 }
2724 }
2825
29- sum += count;
30-
26+ // Append count for this worker
27+ faasmAppendState ( " pi " , BYTES (&count), sizeof ( int ));
3128 return 0 ;
3229}
3330
@@ -40,15 +37,19 @@ int main(int argc, char* argv[])
4037 int nWorkers = std::stoi (inputData);
4138 int nTotal = CHUNK_SIZE * nWorkers;
4239
43- // Initialise the sum
44- auto sum = faasm::AtomicInt (" pi" );
45- sum.reset ();
46-
4740 // Dispatch chained calls
4841 faasmChainBatch (piStep, " " , nWorkers);
4942
50- // Get the final result
51- int finalCount = sum.get ();
43+ // Read in the counts
44+ size_t buffSize = nWorkers * sizeof (int );
45+ auto buffer = new int [nWorkers];
46+ faasmReadAppendedState (" pi" , (uint8_t *)buffer, buffSize, nWorkers);
47+
48+ // Sum the counts
49+ int finalCount = 0 ;
50+ for (int w = 0 ; w < nWorkers; w++) {
51+ finalCount += buffer[w];
52+ }
5253
5354 // Estimate pi
5455 float pi = 4 * ((float )finalCount / (nTotal));
@@ -59,5 +60,6 @@ int main(int argc, char* argv[])
5960 printf (" %s" , output.c_str ());
6061 faasm::setStringOutput (output.c_str ());
6162
63+ delete[] buffer;
6264 return 0 ;
6365}
0 commit comments