How do I set the number of threads in OpenMP [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to set the number of threads within a program using OpenMP. For some reason, even though the maximum number of threads is 4, my program only uses 1 core. I'm on MacOSX but I'm using the gcc compiler (specifically: gcc9.1.0 and OpenMP version 4.5)
#include <fstream>
#include <chrono>
#include <omp.h>
int main() {
int maxthreads = omp_get_max_threads();
std::cout << "maxthreads: " << maxthreads << std::endl;
omp_set_dynamic(0);
omp_set_num_threads(4);
#pragma omp parallel num_threads(4)
{
int id = omp_get_thread_num();
#pragma omp critical
std::cout << "Hi from " << id << std::endl;
}
}
The result that I get is:
4
Hi from 0
But I expected "Hi from i" to be printed 4 times.

I needed to add flags to my cmake:
-DCMAKE_CXX_FLAGS=-fopenmp and -DCMAKE_C_FLAGS=-fopenmp

Related

Apply function that uses matrix elements to update attribute [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I have an Eigen::MatrixXd and I would like to take all the elements in the matrix to update an attribute // within the class. However when I run the following I get errors.
#include <cmath>
#include <iostream>
#include <Eigen/Core>
double x = 0;
void Increment(double x, double increment)
{
value = value + x + increment;
}
int main()
{
Eigen::MatrixXd m(2, 2);
m << 1, 1, 1, 1;
std::cout << m << std::endl << "becomes: ";
std::cout << std::endl << m.unaryExpr(&Exp(1)) << std::endl;
std::cout << value << std::endl << " is the new value";
}
Expecting to get a value of "8" for last cout statement

num_threads clause not setting number of threads [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following simple program
#include <iostream>
#include <omp>
int main() {
std::cout << "max threads: " << omp_get_max_threads() << "\n";
#pragma parallel num_threads(4)
{
int tid = omp_get_thread_num();
std::cout << "Hello from " << tid << " of " << omp_get_num_threads() << "\n";
#pragma omp for
for (int i = 0; i < 5; i++) {
std::cout << "(" << tid << ", " << i << ")\n";
}
}
}
And I am compiling with clang++ -fopenmp=libomp main.cpp. I am able to compile and run other OpenMP programs compiled in this way.
I would expect the num_threads(4) to cause the parallel region to run across 4 threads. Instead I experience the following output:
max threads: 4
Hello from 0 of 1
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
Why is the parallel region not running across 4 threads?
You left the omp out of your parallel pragma.
#pragma omp parallel num_threads(4)

What is wrong in this solution to a codejam quiz? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Link of the question https://code.google.com/codejam/contest/4224486/dashboard
My solution:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int type_A(vector<int>vec){ // function for method 1 of computation;
int ret = 0;
for(auto i = 0;i<vec.size()-1;i++){
int a = vec[i],b=vec[i+1];
if(a>b)ret = ret+(a-b);
}
return ret;
} // end of 1st function
int type_B(vector<int>vec){ // function for method 2 of computation
int ret = 0;
for(auto i = 0;i<vec.size()-1;i++){
if(i==vec.size()-2){
if(vec[i]>vec[i+1])ret+=(vec[i]-vec[i+1]);
}else{
ret += vec[i];
}
}
return ret;
}
// end of function
int main()
{
ifstream input("input_file.in");
ofstream output("output_file.out");
int t;
input>>t;
for(auto i =1;i<=t;i++){
int n;
input>>n;
vector<int>vec(n);
for(auto j = 0;j<vec.size();j++){
int x;
input >>x;
vec[j] =x;
}
output << "Case #" << i << ": " << type_A(vec) << " " << type_B(vec) << endl;
}
}
When I run some examples given with the problems , I get the correct output but when I upload my output file to codejam it says that the answer is incorrect . Please help .
Your algorithm is wrong for type 2. It just happens that all of the examples happen to have the greatest difference items being between the second last item and the last item, which your code relies on (the test for i==vec.size()-2). This is obviously not the case for all of the full tests.
You will need to think of a different algorithm.

Boost log incorrect log [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have a easy struct
struct Test {
std::vector<int> values;
int value;
}
with overloaded << operator
inline std::ostream& operator<<(std::ostream& p, const Test& t)
{
p << "test: ";
for(size_t i = 0; i < t.values.size(); i++) {
std::cout << t.values[i] << " ";
}
p << " value: " << t.value << std::endl;
return p;
}
this works fine when i use the default output. But when i am using my boost logge, shown here Different boost log sinks for every class, it print the values inside my console and the rest inside my file. Anyone has an idea what happens there?
std::cout << t.values[i] << " ";
should be
p << t.values[i] << " ";

micro optimisation in for loop c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's assume C++ for loop in the form of:
for(int i; i<10;i++)
So the integer needs to be allocated in the beginning and then increased at every step the as well as compared. So wouldn't it be faster to do something like that:
f or(int i; i++<10;)
since the variable doesn't need to be loaded into the storage again? espacially when making it volatile?
This little example code gave the same result for all the cases. Does the for loop get optimized anyway or am I missing something?
#include<iostream>
#include<ctime>
int main() {
time_t start,ende;
volatile int dummy = 1;
const int rep = 1000000000;
// Method 1
start = time(0);
for (int i = 0; i < rep; i++)
dummy = 1;
ende = time(0);
std::cout << "Method 1: " << difftime(ende,start)*1000 << " ms" << std::endl;
// Method 2
start = time(0);
for (int i = 0; i++ < rep; )
dummy = 1;
ende = time(0);
std::cout << "Method 2: " << difftime(ende,start)*1000 << " ms" << std::endl;
// Method 3
start = time(0);
for (volatile int i = 0; i < rep; i++)
dummy = 1;
ende = time(0);
std::cout << "Method 3: " << difftime(ende,start)*1000 << " ms" << std::endl;
}
OS: Linux
Compiler: g++
Optimization: standart (no flag)
Compilers are a lot smarter than you think. They won't produce the kind of spurious load that you think they do.
Optimization depends on compiler and its options.
I'd suggest you to disassemble your code and see what's the result of the optimization.
A simple good disassemble e.g. HT editor or IDA version 5 (it free now). For small piece of code it will be easy enough.