• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // This is an example application that uses Marl to find and print all the prime
16 // numbers in the range 1 to 10000000.
17 
18 #include "marl/defer.h"
19 #include "marl/scheduler.h"
20 #include "marl/thread.h"
21 #include "marl/ticket.h"
22 
23 #include <math.h>
24 
25 // searchMax defines the upper limit on primes to find.
26 constexpr int searchMax = 10000000;
27 
28 // searchChunkSize is the number of numbers searched, per task, for primes.
29 constexpr int searchChunkSize = 10000;
30 
31 // isPrime returns true if i is prime.
isPrime(int i)32 bool isPrime(int i) {
33   auto c = static_cast<int>(sqrt(i));
34   for (int j = 2; j <= c; j++) {
35     if (i % j == 0) {
36       return false;
37     }
38   }
39   return true;
40 }
41 
main()42 int main() {
43   // Create a marl scheduler using the full number of logical cpus.
44   // Bind this scheduler to the main thread so we can call marl::schedule()
45   marl::Scheduler scheduler;
46   scheduler.setWorkerThreadCount(marl::Thread::numLogicalCPUs());
47   scheduler.bind();
48   defer(scheduler.unbind());  // unbind before destructing the scheduler.
49 
50   // Create a ticket queue. This will be used to ensure that the primes are
51   // printed in ascending order.
52   marl::Ticket::Queue queue;
53 
54   // Iterate over the range [1, searchMax] in steps of searchChunkSize.
55   for (int searchBase = 1; searchBase <= searchMax;
56        searchBase += searchChunkSize) {
57     // Take a ticket from the ticket queue for this task.
58     auto ticket = queue.take();
59 
60     // Schedule the task.
61     marl::schedule([=] {
62       // Find all the primes in [searchBase, searchBase+searchChunkSize-1].
63       // Note this is run in parallel with the other scheduled tasks.
64       std::vector<int> primes;
65       for (int i = searchBase; i < searchBase + searchChunkSize; i++) {
66         if (isPrime(i)) {
67           primes.push_back(i);
68         }
69       }
70 
71       // Wait until the ticket is called. This ensures that the primes are
72       // printed in ascending order. This may cause this task to yield and allow
73       // other tasks to be executed while waiting for this ticket to be called.
74       ticket.wait();
75 
76       // Print the primes.
77       for (auto prime : primes) {
78         printf("%d is prime\n", prime);
79       }
80 
81       // Call the next ticket so that those primes can be printed.
82       ticket.done();
83     });
84   }
85 
86   // take a ticket and wait on it to ensure that all the primes have been
87   // calculated and printed.
88   queue.take().wait();
89 
90   return 0;
91 }
92