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 parallelize the calculation
16 // of a Julia fractal.
17
18 #include "marl/defer.h"
19 #include "marl/scheduler.h"
20 #include "marl/thread.h"
21 #include "marl/waitgroup.h"
22
23 #include <fstream>
24
25 #include <math.h>
26 #include <stdint.h>
27
28 // A color formed from a red, green and blue component.
29 template <typename T>
30 struct Color {
31 T r, g, b;
32
operator +=Color33 inline Color<T>& operator+=(const Color<T>& rhs) {
34 r += rhs.r;
35 g += rhs.g;
36 b += rhs.b;
37 return *this;
38 }
39
operator /=Color40 inline Color<T>& operator/=(T rhs) {
41 r /= rhs;
42 g /= rhs;
43 b /= rhs;
44 return *this;
45 }
46 };
47
48 // colorize returns a 'rainbow-color' for the scalar v.
colorize(float v)49 inline Color<float> colorize(float v) {
50 constexpr float PI = 3.141592653589793f;
51 constexpr float PI_2_THIRDS = 2.0f * PI / 3.0f;
52 return Color<float>{
53 0.5f + 0.5f * cosf(v + 0 * PI_2_THIRDS),
54 0.5f + 0.5f * cosf(v + 1 * PI_2_THIRDS),
55 0.5f + 0.5f * cosf(v + 2 * PI_2_THIRDS),
56 };
57 }
58
59 // lerp returns the linear interpolation between min and max using the weight x.
lerp(float x,float min,float max)60 inline float lerp(float x, float min, float max) {
61 return min + x * (max - min);
62 }
63
64 // julia calculates the Julia-set fractal value for the given coordinate and
65 // constant. See https://en.wikipedia.org/wiki/Julia_set for more information.
julia(float x,float y,float cx,float cy)66 Color<float> julia(float x, float y, float cx, float cy) {
67 for (int i = 0; i < 1000; i++) {
68 if (x * x + y * y > 4) {
69 return colorize(sqrtf(static_cast<float>(i)));
70 }
71
72 auto xtemp = x * x - y * y;
73 y = 2 * x * y + cy;
74 x = xtemp + cx;
75 }
76
77 return {};
78 }
79
80 // writeBMP writes the given image as a bitmap to the given file, returning
81 // true on success and false on error.
writeBMP(const Color<uint8_t> * texels,int width,int height,const char * path)82 bool writeBMP(const Color<uint8_t>* texels,
83 int width,
84 int height,
85 const char* path) {
86 auto file = fopen(path, "wb");
87 if (!file) {
88 fprintf(stderr, "Could not open file '%s'\n", path);
89 return false;
90 }
91 defer(fclose(file));
92
93 bool ok = true;
94 auto put4 = [&](uint32_t val) { ok = ok && fwrite(&val, 1, 4, file) == 4; };
95 auto put2 = [&](uint16_t val) { ok = ok && fwrite(&val, 1, 2, file) == 2; };
96 auto put1 = [&](uint8_t val) { ok = ok && fwrite(&val, 1, 1, file) == 1; };
97
98 const uint32_t padding = -(3 * width) & 3U; // in bytes
99 const uint32_t stride = 3 * width + padding; // in bytes
100 const uint32_t offset = 54;
101
102 // Bitmap file header
103 put1('B'); // header field
104 put1('M');
105 put4(offset + stride * height * 3); // size in bytes
106 put4(0); // reserved
107 put4(offset);
108
109 // BITMAPINFOHEADER
110 put4(40); // size of header in bytes
111 put4(width); // width in pixels
112 put4(height); // height in pixels
113 put2(1); // number of color planes
114 put2(24); // bits per pixel
115 put4(0); // compression scheme (none)
116 put4(0); // size
117 put4(72); // horizontal resolution
118 put4(72); // vertical resolution
119 put4(0); // color pallete size
120 put4(0); // 'important colors' count
121
122 for (int y = height - 1; y >= 0; y--) {
123 for (int x = 0; x < width; x++) {
124 auto& texel = texels[x + y * width];
125 put1(texel.b);
126 put1(texel.g);
127 put1(texel.r);
128 }
129 for (uint32_t i = 0; i < padding; i++) {
130 put1(0);
131 }
132 }
133
134 return ok;
135 }
136
137 // Constants used for rendering the fractal.
138 constexpr uint32_t imageWidth = 2048;
139 constexpr uint32_t imageHeight = 2048;
140 constexpr int samplesPerPixelW = 3;
141 constexpr int samplesPerPixelH = 3;
142 constexpr float windowMinX = -0.5f;
143 constexpr float windowMaxX = +0.5f;
144 constexpr float windowMinY = -0.5f;
145 constexpr float windowMaxY = +0.5f;
146 constexpr float cx = -0.8f;
147 constexpr float cy = 0.156f;
148
main()149 int main() {
150 // Create a marl scheduler using the full number of logical cpus.
151 // Bind this scheduler to the main thread so we can call marl::schedule()
152 marl::Scheduler scheduler;
153 scheduler.setWorkerThreadCount(marl::Thread::numLogicalCPUs());
154 scheduler.bind();
155 defer(scheduler.unbind()); // unbind before destructing the scheduler.
156
157 // Allocate the image.
158 auto pixels = new Color<uint8_t>[imageWidth * imageHeight];
159 defer(delete[] pixels); // free memory before returning.
160
161 // Create a wait group that will be used to synchronize the tasks.
162 // The wait group is constructed with an initial count of imageHeight as
163 // there will be a total of imageHeight tasks.
164 marl::WaitGroup wg(imageHeight);
165
166 // For each line of the image...
167 for (uint32_t y = 0; y < imageHeight; y++) {
168 // Schedule a task to calculate the image for this line.
169 // These may run concurrently across hardware threads.
170 marl::schedule([=] {
171 // Before this task returns, decrement the wait group counter.
172 // This is used to indicate that the task is done.
173 defer(wg.done());
174
175 for (uint32_t x = 0; x < imageWidth; x++) {
176 // Calculate the fractal pixel color.
177 Color<float> color = {};
178 // Take a number of sub-pixel samples.
179 for (int sy = 0; sy < samplesPerPixelH; sy++) {
180 auto fy = float(y) + (sy / float(samplesPerPixelH));
181 auto dy = float(fy) / float(imageHeight);
182 for (int sx = 0; sx < samplesPerPixelW; sx++) {
183 auto fx = float(x) + (sx / float(samplesPerPixelW));
184 auto dx = float(fx) / float(imageWidth);
185 color += julia(lerp(dx, windowMinX, windowMaxX),
186 lerp(dy, windowMinY, windowMaxY), cx, cy);
187 }
188 }
189 // Average the color.
190 color /= samplesPerPixelW * samplesPerPixelH;
191 // Write the pixel out to the image buffer.
192 pixels[x + y * imageWidth] = {static_cast<uint8_t>(color.r * 255),
193 static_cast<uint8_t>(color.g * 255),
194 static_cast<uint8_t>(color.b * 255)};
195 }
196 });
197 }
198
199 // Wait until all image lines have been calculated.
200 wg.wait();
201
202 // Write the image to "fractal.bmp".
203 if (!writeBMP(pixels, imageWidth, imageHeight, "fractal.bmp")) {
204 return 1;
205 }
206
207 // All done.
208 return 0;
209 }
210