Skip to content

Commit

Permalink
add 添加加法的并行计算示例
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaominghe2014 committed Sep 15, 2023
1 parent 5384848 commit d286627
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/math/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace xlib {
//最小公倍数
int lcm(int a,int b);
int lcm(const std::vector<int>&);

//多线程并行计算和
int parallelSum(const std::vector<int>& nums);
}

#endif /* util_h */
26 changes: 25 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ void setLog()
LOG_SET(LOG_LEVEL::L_ALL);
LOG_I("log dir:%s", xlib_log_dir.c_str());
XLog::setWrite(true, xlib_log_dir.append("xlib.log"));

int numThreads = std::thread::hardware_concurrency();

LOG_I("numThreads = %d",numThreads);
}

void testHttp()
Expand Down Expand Up @@ -498,6 +502,26 @@ void testMath()
auto com = combination(arr, 2);

LOG_I("combination:\n%s", XString::toString(com).c_str());

std::vector<int> nums = {};
for(int i = 0 ; i< 1000000; i++){
nums.push_back(i);
}
auto start_time = std::chrono::high_resolution_clock::now();
int t = 0;
for(auto e: nums){
t+=e;
}
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed_time = end_time - start_time;

LOG_I("Single compute:time:%f 毫秒,result:%d",elapsed_time.count(),t);

auto parallel_start_time = std::chrono::high_resolution_clock::now();
int parallel_total = parallelSum(nums);
auto parallel_end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> parallel_elapsed_time = parallel_end_time - parallel_start_time;
LOG_I("Parallel compute sum :time:%f 毫秒,result:%d",parallel_elapsed_time.count(),parallel_total);
}

void testGaussianElimination()
Expand Down Expand Up @@ -676,6 +700,6 @@ int main(int argc, char *argv[])
testUrl();
testSort();
testSolveStandardSudoku();
testDecodeAndEncode();
testDecodeAndEncode();
return 0;
}
38 changes: 37 additions & 1 deletion src/math/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//

#include "math/math.h"
#include <thread>
#include <future>

namespace xlib {

Expand Down Expand Up @@ -114,7 +116,7 @@ namespace xlib {
if(0==a || b==0) return 0;
a = a<0? -a:a;
b = b<0? -b:b;
return a*b / gcd(a, b);
return a / gcd(a, b)*b;
}

int lcm(const std::vector<int>& nums){
Expand All @@ -128,4 +130,38 @@ namespace xlib {
}
return result;
}

int parallelSum(const std::vector<int>& nums) {
int sum = 0;

std::vector<std::future<int>> futures;

int numThreads = std::thread::hardware_concurrency();
if(numThreads<2){
for(int n:nums){
sum+=n;
}
return sum;
}
int chunkSize = nums.size() / numThreads;

for (int i = 0; i < numThreads; i++) {
int start = i * chunkSize;
int end = (i == numThreads - 1) ? nums.size() : (i + 1) * chunkSize;

futures.push_back(std::async(std::launch::async, [start, end, &nums]() {
int localSum = 0;
for (int j = start; j < end; j++) {
localSum += nums[j];
}
return localSum;
}));
}

for (auto& future : futures) {
sum += future.get();
}

return sum;
}
}

0 comments on commit d286627

Please sign in to comment.