├── .gitignore ├── stockast.vcxproj.user ├── stockast.vcxproj.filters ├── LICENSE ├── stockast.sln ├── Makefile ├── profiling.sh ├── data.csv ├── README.md ├── stockast.vcxproj └── stockast.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .vs/* 2 | opt.csv 3 | x64/* 4 | *.o 5 | stockast 6 | -------------------------------------------------------------------------------- /stockast.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /stockast.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2023 Rajdeep Konwar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /stockast.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.168 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stockast", "stockast.vcxproj", "{FDE95EFC-FD7D-4C60-92DB-58573E7074FF}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Debug|x64.ActiveCfg = Debug|x64 17 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Debug|x64.Build.0 = Debug|x64 18 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Debug|x86.ActiveCfg = Debug|Win32 19 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Debug|x86.Build.0 = Debug|Win32 20 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Release|x64.ActiveCfg = Release|x64 21 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Release|x64.Build.0 = Release|x64 22 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Release|x86.ActiveCfg = Release|Win32 23 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {041EBC52-6B5F-4A3A-91D4-88BF17625364} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | # @file This file is part of stockast. 3 | # 4 | # @section LICENSE 5 | # MIT License 6 | # 7 | # Copyright (c) 2017-2023 Rajdeep Konwar 8 | # 9 | # Permission is hereby granted, free of charge, to any person obtaining a copy 10 | # of this software and associated documentation files (the "Software"), to deal 11 | # in the Software without restriction, including without limitation the rights 12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | # copies of the Software, and to permit persons to whom the Software is 14 | # furnished to do so, subject to the following conditions: 15 | # 16 | # The above copyright notice and this permission notice shall be included in all 17 | # copies or substantial portions of the Software. 18 | # 19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | # SOFTWARE. 26 | # 27 | # @section DESCRIPTION 28 | # Makefile. 29 | ## 30 | 31 | CXX ?= g++ 32 | CXXFLAGS=-std=c++14 -O3 -fopenmp 33 | LDFLAGS=-fopenmp 34 | 35 | OBJECTS=stockast.o 36 | 37 | stockast: $(OBJECTS) 38 | $(CXX) $(CXXFLAGS) $(OBJECTS) -o stockast $(LDFLAGS) 39 | 40 | stockast.o: stockast.cpp 41 | 42 | .PHONY: clean 43 | 44 | clean: 45 | rm -f *.o stockast 46 | -------------------------------------------------------------------------------- /profiling.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | ## 3 | # @file This file is part of stockast. 4 | # 5 | # @section LICENSE 6 | # MIT License 7 | # 8 | # Copyright (c) 2017-2023 Rajdeep Konwar 9 | # 10 | # Permission is hereby granted, free of charge, to any person obtaining a copy 11 | # of this software and associated documentation files (the "Software"), to deal 12 | # in the Software without restriction, including without limitation the rights 13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | # copies of the Software, and to permit persons to whom the Software is 15 | # furnished to do so, subject to the following conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in all 18 | # copies or substantial portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | # SOFTWARE. 27 | # 28 | # @section DESCRIPTION 29 | # Profiling script for different number of threads used. 30 | ## 31 | 32 | if [ "$#" -ne 1 ]; then 33 | echo "Usage: $0 " >&2 34 | exit 1 35 | fi 36 | 37 | > time_local.log 38 | for ((i = 1; i <=$1; i++)); 39 | do 40 | echo $i; 41 | export OMP_NUM_THREADS=$i; 42 | time ./stockast >> time_local.log; 43 | done 44 | -------------------------------------------------------------------------------- /data.csv: -------------------------------------------------------------------------------- 1 | 99.80311647,99.72990912,99.65636726,99.6556732,99.69786154,99.74150906,99.73389665,99.79820824,99.79761168,99.79823002,99.79823002,99.78262647,99.78262647,99.78262647,99.73969133,99.73773184,99.73797132,99.73490099,99.73490099,99.73490099,99.734149,99.734149,99.77562657,99.7592583,99.75836796,99.71464245,99.71464245,99.71562527,99.64409042,99.62799208,99.62799208,99.62799208,99.62799208,99.57002365,99.66995111,99.66896711,99.68228756,99.68365737,99.66949599,99.56961951,99.56893026,99.56844997,99.56717845,99.56717845,99.63750043,99.56438822,99.49161966,99.29320141,99.27805847,99.29122139,99.29122139,99.29122139,99.29057403,99.36138729,99.36223967,99.36223967,99.31930126,99.20495047,99.13329905,99.13339801,98.77920303,98.82217857,98.82098415,98.80793197,98.80830788,98.84958174,98.84852822,98.84852822,98.57965834,98.28474207,98.29742855,98.29860256,98.29842983,98.28496445,97.93797363,97.92403829,97.93724476,97.93919152,97.92627805,97.97024764,97.96947664,97.97006179,97.99765621,97.98403421,97.97144908,97.97192716,97.97139071,97.80561647,97.59646317,97.736,97.8226,97.8727,97.872,97.8722,97.8693,97.8675,97.8656,97.8617,97.8596,97.8676,97.8772,97.8849,97.8893,97.8925,97.8952,97.8981,97.9011,97.9043,97.9078,97.9115,97.9153,97.919,97.9225,97.9257,97.9289,97.9321,97.9352,97.9384,97.9415,97.9446,97.9477,97.9507,97.9537,97.9566,97.9595,97.9623,97.9652,97.968,97.9707,97.9735,97.9762,97.9788,97.9815,97.984,97.9866,97.9891,97.9916,97.9941,97.9965,97.9989,98.0013,98.0036,98.0059,98.0082,98.0105,98.0127,98.0149,98.0171,98.0192,98.0214,98.0234,98.0255,98.0275,98.0295,98.0316,98.0335,98.0354,98.0374,98.0393,98.0411,98.043,98.0448,98.0466,98.0484,98.0501,98.0519,98.0536,98.0553,98.0569,98.0586,98.0602,98.0618,98.0634,98.0649,98.0665,98.0681,98.0696,98.0711,98.0726 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stockast [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FRajdeepKonwar%2Fstockast.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FRajdeepKonwar%2Fstockast?ref=badge_shield) 2 | ## Stock Market Forecasting using Parallel (OpenMP) Monte-Carlo Simulations 3 | 4 | ![alt text](https://i.imgur.com/dHf0aRO.png) 5 | 6 | ### Compile Instructions 7 | #### Windows 8 | * Open `stockast.sln` 9 | * [Optional] Right-click Solution 'stockast' in the Solution Explorer and select `Retarget solution` 10 | * Build and run! 11 | 12 | #### Linux 13 | ``` 14 | make 15 | ``` 16 | Type `make clean` to clean object file and executable. 17 | 18 | ### Run Instructions 19 | #### Windows 20 | Simply run from Visual Studio or double-click the executable created inside x64\\{config}\stockast.exe 21 | 22 | By default, the program will try and utilize the maximum system threads available. In order to use a specific number of threads, set the environment vairable `OMP_NUM_THREADS` equal to the number of threads you want. 23 | 24 | #### Linux 25 | Set the number of threads to be used for computation, 26 | ``` 27 | export OMP_NUM_THREADS=number_of_threads 28 | ``` 29 | For example, `export OMP_NUM_THREADS=8`. 30 | Then run the program 31 | ``` 32 | ./stockast 33 | ``` 34 | 35 | ![image](https://user-images.githubusercontent.com/22571164/212611578-cec8eecf-a858-467d-967b-79315ea33d60.png) 36 | 37 | ### General info 38 | * The input file "data.csv" contains the stock-price values for 3 hours prior to run-time; this acts as the history-data and helps estimate the market volatility. 39 | * The output file "opt.csv" contains the output (most likely outcome) price-vector from our code. One can use Excel or gnuplot to plot the resulting line graph of the predicted stock pricing. 40 | * (**Linux only**) The script "profiling.sh" runs the parallel code from 1 to the specified number of threads. To use the script, 41 | ``` 42 | ./profiling.sh "number_of_threads" 43 | ``` 44 | For example, `./profiling.sh 8`. 45 | -------------------------------------------------------------------------------- /stockast.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {FDE95EFC-FD7D-4C60-92DB-58573E7074FF} 24 | stockast 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v143 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v143 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v143 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v143 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | Disabled 77 | true 78 | true 79 | true 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | true 88 | true 89 | 90 | 91 | 92 | 93 | Level3 94 | MaxSpeed 95 | true 96 | true 97 | true 98 | true 99 | true 100 | 101 | 102 | true 103 | true 104 | 105 | 106 | 107 | 108 | Level3 109 | MaxSpeed 110 | true 111 | true 112 | true 113 | true 114 | true 115 | 116 | 117 | true 118 | true 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /stockast.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file This file is part of stockast. 3 | * 4 | * @section LICENSE 5 | * MIT License 6 | * 7 | * Copyright (c) 2017-2023 Rajdeep Konwar 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | * 27 | * @section DESCRIPTION 28 | * Stock Market Forecasting using parallel Monte-Carlo simulations 29 | * (src:wikipedia) The Black–Scholes model assumes that the market consists of 30 | * at least one risky asset, usually called the stock, and one riskless asset, 31 | * usually called the money market, cash, or bond. The rate of return on the 32 | * riskless asset is constant and thus called the risk-free interest rate. 33 | **/ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | //---------------------------------------------------------------------------- 47 | // Calculates volatility from data.csv file 48 | //---------------------------------------------------------------------------- 49 | float calculateVolatility(float spotPrice, int32_t timeSteps) 50 | { 51 | // Open data.csv in read-mode, exit on fail 52 | std::ifstream filePtr; 53 | filePtr.open("data.csv", std::ifstream::in); 54 | if (!filePtr.is_open()) 55 | { 56 | std::cerr << "Cannot open data.csv! Exiting..\n"; 57 | exit(EXIT_FAILURE); 58 | } 59 | 60 | std::string line; 61 | // Read the first line then close file 62 | if (!std::getline(filePtr, line)) 63 | { 64 | std::cerr << "Cannot read from data.csv! Exiting..\n"; 65 | filePtr.close(); 66 | exit(EXIT_FAILURE); 67 | } 68 | filePtr.close(); 69 | 70 | int32_t i = 0, len = timeSteps - 1; 71 | std::unique_ptr priceArr = std::make_unique(timeSteps - 1); 72 | std::istringstream iss(line); 73 | std::string token; 74 | 75 | // Get the return values of stock from file (min 2 to 180) 76 | while (std::getline(iss, token, ',')) 77 | priceArr[i++] = std::stof(token); 78 | 79 | float sum = spotPrice; 80 | // Find mean of the estimated minute-end prices 81 | for (i = 0; i < len; i++) 82 | sum += priceArr[i]; 83 | float meanPrice = sum / (len + 1); 84 | 85 | // Calculate market volatility as standard deviation 86 | sum = std::powf((spotPrice - meanPrice), 2.0f); 87 | for (i = 0; i < len; i++) 88 | sum += std::powf((priceArr[i] - meanPrice), 2.0f); 89 | 90 | float stdDev = std::sqrtf(sum); 91 | 92 | // Return as percentage 93 | return stdDev / 100.0f; 94 | } 95 | 96 | /** --------------------------------------------------------------------------- 97 | Finds mean of a 2D array across first index (inLoops) 98 | M is in/outLoops and N is timeSteps 99 | ----------------------------------------------------------------------------*/ 100 | float* find2dMean(float** matrix, int32_t numLoops, int32_t timeSteps) 101 | { 102 | int32_t j; 103 | float* avg = new float[timeSteps]; 104 | float sum = 0.0f; 105 | 106 | for (int32_t i = 0; i < timeSteps; i++) 107 | { 108 | /** A private copy of 'sum' variable is created for each thread. 109 | At the end of the reduction, the reduction variable is applied to 110 | all private copies of the shared variable, and the final result 111 | is written to the global shared variable. **/ 112 | #pragma omp parallel for private(j) reduction(+:sum) 113 | for (j = 0; j < numLoops; j++) 114 | { 115 | sum += matrix[j][i]; 116 | } 117 | 118 | // Calculating average across columns 119 | avg[i] = sum / numLoops; 120 | sum = 0.0f; 121 | } 122 | 123 | return avg; 124 | } 125 | 126 | /** --------------------------------------------------------------------------- 127 | Generates a random number seeded by system clock based on standard 128 | normal distribution on taking mean 0.0 and standard deviation 1.0 129 | ----------------------------------------------------------------------------*/ 130 | float genRand(float mean, float stdDev) 131 | { 132 | const auto seed = std::chrono::system_clock::now().time_since_epoch().count(); 133 | std::default_random_engine generator(static_cast(seed)); 134 | std::normal_distribution distribution(mean, stdDev); 135 | return distribution(generator); 136 | } 137 | 138 | //---------------------------------------------------------------------------- 139 | // Simulates Black Scholes model 140 | //---------------------------------------------------------------------------- 141 | float* runBlackScholesModel(float spotPrice, int32_t timeSteps, float riskRate, float volatility) 142 | { 143 | static constexpr float mean = 0.0f, stdDev = 1.0f; // Mean and standard deviation 144 | float deltaT = 1.0f / timeSteps; // Timestep 145 | std::unique_ptr normRand = std::make_unique(timeSteps - 1); // Array of normally distributed random nos. 146 | float* stockPrice = new float[timeSteps]; // Array of stock price at diff. times 147 | stockPrice[0] = spotPrice; // Stock price at t=0 is spot price 148 | 149 | // Populate array with random nos. 150 | for (int32_t i = 0; i < timeSteps - 1; i++) 151 | normRand[i] = genRand(mean, stdDev); 152 | 153 | // Apply Black Scholes equation to calculate stock price at next timestep 154 | for (int32_t i = 0; i < timeSteps - 1; i++) 155 | stockPrice[i + 1] = stockPrice[i] * exp(((riskRate - (std::powf(volatility, 2.0f) / 2.0f)) * deltaT) + (volatility * normRand[i] * std::sqrtf(deltaT))); 156 | 157 | return stockPrice; 158 | } 159 | 160 | //---------------------------------------------------------------------------- 161 | // Main function 162 | //---------------------------------------------------------------------------- 163 | int32_t main(int32_t argc, char** argv) 164 | { 165 | const auto beginTime = std::chrono::system_clock::now(); 166 | 167 | static constexpr int32_t inLoops = 100; // Inner loop iterations 168 | static constexpr int32_t outLoops = 10000; // Outer loop iterations 169 | static constexpr int32_t timeSteps = 180; // Stock market time-intervals (min) 170 | 171 | // Matrix for stock-price vectors per iteration 172 | float** stock = new float* [inLoops]; 173 | for (int32_t i = 0; i < inLoops; i++) 174 | stock[i] = new float[timeSteps]; 175 | 176 | // Matrix for mean of stock-price vectors per iteration 177 | float** avgStock = new float* [outLoops]; 178 | for (int32_t i = 0; i < outLoops; i++) 179 | avgStock[i] = new float[timeSteps]; 180 | 181 | static constexpr float spotPrice = 100.0f; // Spot price (at t = 0) 182 | 183 | // Market volatility (calculated from data.csv) 184 | const float volatility = calculateVolatility(spotPrice, timeSteps); 185 | 186 | // Welcome message 187 | std::cout << "==============================================\n"; 188 | std::cout << " Stockast - Stock Forecasting Tool\n"; 189 | std::cout << " Copyright (c) 2017-2023 Rajdeep Konwar\n"; 190 | std::cout << "==============================================\n\n"; 191 | std::cout << "Using market volatility: " << volatility << "\n"; 192 | 193 | int32_t i; 194 | // Parallel region with each thread having its own instance of variable 'i', 195 | #pragma omp parallel private(i) 196 | { 197 | // Only one thread (irrespective of thread id) handles this region 198 | #pragma omp single 199 | { 200 | const int32_t numThreads = omp_get_num_threads(); // Number of threads 201 | std::cout << "Using " << numThreads << " thread(s)\n\n"; 202 | std::cout << "Have patience! Computing.. "; 203 | omp_set_num_threads(numThreads); 204 | } 205 | 206 | /** Parallel for loop with dynamic scheduling, i.e. each thread 207 | grabs "chunk" iterations until all iterations are done. 208 | Faster threads are assigned more iterations (not Round Robin) **/ 209 | #pragma omp for schedule(dynamic) 210 | for (i = 0; i < outLoops; i++) 211 | { 212 | /** Using Black Scholes model to get stock price every iteration 213 | Returns data as a column vector having rows=timeSteps **/ 214 | for (int32_t j = 0; j < inLoops; j++) 215 | { 216 | static constexpr float riskRate = 0.001f; // Risk free interest rate (%) 217 | stock[j] = runBlackScholesModel(spotPrice, timeSteps, riskRate, volatility); 218 | } 219 | 220 | // Stores average of all estimated stock-price arrays 221 | avgStock[i] = find2dMean(stock, inLoops, timeSteps); 222 | } 223 | //---> Implicit omp barrier <-- 224 | } 225 | 226 | // Average of all the average arrays 227 | float *optStock = new float[timeSteps]; // Vector for most likely outcome stock price 228 | optStock = find2dMean(avgStock, outLoops, timeSteps); 229 | 230 | // Write optimal outcome to disk 231 | std::ofstream filePtr; 232 | filePtr.open("opt.csv", std::ofstream::out); 233 | if (!filePtr.is_open()) 234 | { 235 | std::cerr << "Couldn't open opt.csv! Exiting..\n"; 236 | return EXIT_FAILURE; 237 | } 238 | 239 | for (i = 0; i < timeSteps; i++) 240 | filePtr << optStock[i] << "\n"; 241 | filePtr.close(); 242 | 243 | for (i = 0; i < inLoops; i++) 244 | delete[] stock[i]; 245 | delete[] stock; 246 | 247 | for (i = 0; i < outLoops; i++) 248 | delete[] avgStock[i]; 249 | delete[] avgStock; 250 | 251 | delete[] optStock; 252 | 253 | std::cout << "done!\nTime taken: " << std::to_string(std::chrono::duration_cast(std::chrono::system_clock::now() - beginTime).count()) << "s"; 254 | return std::getchar(); 255 | } 256 | --------------------------------------------------------------------------------