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 years ago.
Improve this question
I was doing a project that uses threads to sum some matrix but at the time of creating the threads and adding the params it always shows up the same error. Any ideas?
void sum(std::vector <double>& matrix, std::vector <double>& other) {
for (auto i = 0; i < 15; i++) {
matrix[i] += other[i];
}
}
here it is the operation that threads should do.
std::vector <double>* mat1 = new std::vector <double>[15];
std::vector <double>* mat2 = new std::vector <double>[15];
std::vector <std::thread*> threads;
for (int j = 0; j < 15; j++) {
sum(mat1[j], mat2[j]); //this works;
threads.push_back(new std::thread(sum,mat1[j],mat2[j])); //this dont why?;
}
Thanks in advance
To get it to compile, change:
std::thread(sum,mat1[j],mat2[j])
to:
std::thread(sum, std::ref(mat1[j]), std::ref(mat2[j]))
Example: https://godbolt.org/z/Ek-cnm
But there are multiple problems with your question and code besides just getting it to compile, please listen to what other have said in the comments.
Related
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 1 year ago.
Improve this question
I just started Data Structures and i have stumbled upon Priority queues, I wrote a simple program to print the values that i have pushed in the Queues but it wont print anything.
#include<iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> maxi;
priority_queue<int, vector<int>, greater<int>> mini;
int m = maxi.size();
int n = mini.size();
maxi.push(1);
maxi.push(3);
maxi.push(2);
maxi.push(5);
maxi.push(0);
cout<<"Max size->"<<maxi.size()<<endl;
for(int i=0; i<m; i++){
cout<<maxi.top()<<" ";
maxi.pop();
}cout<<endl;
mini.push(1);
mini.push(3);
mini.push(2);
mini.push(5);
mini.push(0);
cout<<"Mini size->"<<mini.size()<<endl;
for(int j=0; j<n; j++){
cout<<mini.top()<<" ";
mini.pop();
}cout<<endl;
}
I have read so many articles i just dont seem to find any error. Even the Compiler doesnt give any error.
You are getting the size of maxi and mini before adding elements to them. Since you're copying the value of the size (explained here), it wouldn't be updated when adding elements to the containers. Therefore your loop is not executed at all.
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 2 years ago.
Improve this question
Im trying to opptimise this peice of code as its a small section of a longer code for speed rather than memory. How best would I do that. I was thinking to use set the v_vtx vector to be able to just to define the chitemp array.
double chitemp[nvert1][2];
for (int i=0;i<nvert1;i++){
chitemp[i][1]=v_vtx[i];
chitemp[i][0]=chi2->at(v_vtx[i]);
}
for (int k = 0; k < nvert1; k++){
for( int p = k+1; p < nvert1; p++){
if( chitemp[k][0] > chitemp[p][0]){
swap(chitemp[k][0], chitemp[p][0]);
swap(chitemp[k][1], chitemp[p][1]);
}
}
}
edit:
Im trying to sort chi2 (double) into order and know which v_vtx (int) links to the chi2 value
You could instead store your values as pairs (using std::array is optional, but offers a richer interface than an inbuilt array):
std::array<std::pair<double>, nvert1> chitemp;
for (size_t i = 0; i < nvert1; ++i) {
chitemp[i].second = v_vtx[i];
chitemp[i].first = chi2->at(v_vtx[i]);
}
Then, use...
std::sort(chitemp.begin(), chitemp.end());
...instead of your (inefficient) home-grown bubble-sort.
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 5 years ago.
Improve this question
I have a big vector and i want to update some data in that vector (no insert/delete, but rather replacing an element with another on a specified index).
I was thinking that this is pretty clever to do the work on 2 or more different threads, therefore improving the speed. And since no synchronization is really necessary in this case, due to different indices this should be really fast.
Unfortunately my code crashes, either by stating: EXC_BAD_ACCESS, or "pointer being freed was not allocated".
The pseudocode:
// I have an entries_ vector with data of type DataT
std::vector<std::thread> workers(NUMBER_OF_PARALLEL_CHUNKS);
unsigned long tuplesPerChunk = entries_.size() / NUMBER_OF_PARALLEL_CHUNKS;
for (int j = 0; j < NUMBER_OF_PARALLEL_CHUNKS; ++j) {
unsigned long offset = tuplesPerChunk * j;
workers.emplace_back(std::thread([&offset, &tuplesPerChunk, this](){
for (int i = 0; i < tuplesPerChunk; ++i) {
unsigned long offsetIndex = offset + i;
entries_[offsetIndex] = createNewDataForSomeParticularReason();
}
}));
}
for (auto &worker : workers) {
if (worker.joinable()) worker.join();
}
Capture offset by value, else you have dangling pointer.
Your threads live from inside the loop until the join.
offset only lives inside one loop iteration.
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 6 years ago.
Improve this question
I don't understand how to iterate only on a portion of an Stl map and not from begin to end like in standard traversate. Here is my code:
auto end = temp_map.rbegin() + THRESHOLD_NUM;
for (auto rit = temp_map.rbegin(); rit != end; ++rit)
{
int s = rit->second;
for (int k = 0; k < MAX_ROWS; k++)
{
array_dist_it[k] = abs(input[k] - input_matrix[k][s]);
}
float av_real = mean(MAX_ROWS, array_dist_it);
float score_real = score_func(av_real);
rank_function(score_real, s);
}
}
I think that the problem is related to the syntax of the for loop and in particular to the iterator. The error is about an invalid operator.
A std::map has a BidirectionalIterator. It supports incrementing and decrementing but not addition or subtraction. If you need to advance and iterator N times then you can use std::next. Using that instead of
auto end = temp_map.rbegin() + THRESHOLD_NUM;
You would have
auto end = std::next(temp_map.rbegin(), THRESHOLD_NUM);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
can someone look at this code and tell me if I am creating the pointer and object correctly please.
int main()
{
Square<int>* originalSquare = new Square<int>(3, 3);
for(int r = 0; r < originalSquare -> rowSize; r++)
{
for(int c = 0; c < originalSquare -> colSize; c++)
{
int num= 0;
originalSquare -> setElement(r, c, num);
}
}
return 0;
}
//quick_sort function
void quick_sort(Square<int>* square)
{
//nothing yet.
}
I keep getting a access violation error for somer reason... Program works fine before I changed this from stack to heap...
Any help will be greatful.
Thanks
This is not the code that shows your problem. Although, I would guess Square allocates a dynamically sized array, and setElement sets it? Can we see your constructor, and the code for setElement?