parallel processing for C+11's vector - c++

When I try to do average images input a vector asynchronously(for example, concurrency::concurrent_vector<cv::Mat>), How Can I parallelize sum about points or batches(1 row or 1 col or 3*3 array) of the same coordinates or Area?
I would appreciate it if you could tell me how to calculate the values in vector in columns or batch rather than in single units(ex. nested for).
(Edit)
For example
If I have 3 thread for image processing, and Each result are
thread 1
1 1 1
1 1 1
1 1 1
and thread 2
2 2 2
2 2 2
2 2 2
thread 3
6 6 6
6 6 6
6 6 6
then, just I want is
3 3 3
3 3 3
3 3 3
I thought two way for calculate average all thread's image.
1. just sum each thread result derivered to main thread and
count how much result derivered.
If thread1&2 result derivered to main thread.
(1) sum
3 3 3
3 3 3
3 3 3
(2) save count of sum and coordinate
In this example, save value will
1 - count of sum
Rect(0, 0, 2, 2) - coordinate to nested area
(3) If all thread's result coming, do average about nested area
9 9 9
9 9 9
9 9 9
if count of sum value is 2, find nested area and do average.
2(count of sum) / Rect(0, 0, 2, 2)
result will be
3 3 3
3 3 3
3 3 3
2. Just wait all thread's result derivered and do average in batches.
like
1|1 1
1|1 1
1|1 1
2|2 2
2|2 2
2|2 2
6|6 6
6|6 6
6|6 6
|
9
9
9
|
3
3
3
But, I don't know how to access and calculate by Memory References each thread's images. If thread 1 image address (in this case, 0,0 pixel data address in image) is 100, and thread 2 image address start is 200. then (0,0) pixel data in result images will calculate *100+*200.
Of course, before doing this operation, I will have to check that the memory matching the coordinates has the correct value.
And, Who told If I use std::reduce, will easy to implementation about this.
But, I have no idea how to apply that function in this way.

Related

Number of first element insertions to sort array in O(nlogn)?

If you am only allowed to move the first element of an array, how many insertions does it take to fully sort the array?
In the output, give the number of insertions necessary as well as how many positions each element moves back.
For example:
Input:
6
1 4 2 5 3 6
Output:
4
3 4 2 4
Explanation:
This is the order of insertions:
4 2 5 1 3 6
2 5 1 3 4 6
5 1 2 3 4 6
1 2 3 4 5 6
I can do this in O(n2) since the problem simplifies to finding the position where the first element lies in the increasing suffix of the array.
How can I solve this in O(nlogn)?

Meshing options to generate number of the sides of and element (tetgen-triangle)

I wrote a finite element code in fortran 90.
This code is really fast, except the meshing process.
I used triangle and tetgen for meshing in 2D and 3D, respectively, so this process is fast, of course.
For example, for the unit square [0,1]x[0,1] in 2D I have a file with the coordinates of its nodes (for example, a mesh with 5 nodes):
1 0.0 0.0 # coordinates of node 1
2 1.0 0.0 # coordinates of node 2
3 1.0 1.0 # coordinates of node 3
4 0.0 1.0 # coordinates of node 4
5 0.5 0.5 # coordinates of node 5
called coordinate.dat, which have 4 elements (triangles) with nodes called element.dat
1 1 5 4 # vertices of triangle 1
2 1 2 5 # vertices of triangle 2
3 2 3 5 # vertices of triangle 3
4 5 2 4 # vertices of triangle 4
I also have a file where each row i is the number of its initial an final node, called edge.dat:
1 1 2 # initial and final node of edge 1
2 2 3 # initial and final node of edge 2
3 3 4 # initial and final node of edge 3
4 4 1 # initial and final node of edge 4
5 1 5 # initial and final node of edge 5
6 5 2 # initial and final node of edge 6
7 2 5 # initial and final node of edge 7
8 5 4 # initial and final node of edge 8
With this files, I need to generate the following information:
(1) Given an element (triangle or tetrahedron), I need to know the number of its sides (edges and faces, respectively). For example, I need to generate the following structure or file, called struct1.dat:
1 5 8 4 # triangle 1 has the edges number 5, 8 and 4
2 1 6 5 # triangle 2 has the edges number 1, 6 and 5
3 6 2 7 # triangle 2 has the edges number 6, 2 and 7
4 7 3 8 # triangle 4 has the edges number 7, 3 and 8
(2) Furthermore, given a side (edge or face) I need to know the element numbers of the 2 elements (or only one if the side is on the boundary) shared by that side. For example, I need to generate the following structure (or file) called struct2.dat:
1 2 0 # edge number 1 is only on element 2
2 3 0 # edge number 2 is only on element 3
3 4 0 # edge number 3 is only on element 4
4 1 0 # edge number 4 is only on element 1
5 1 2 # edge number 5 is sharing by elements 1 and 2
6 3 2 # edge number 6 is sharing by elements 3 and 2
7 4 3 # edge number 7 is sharing by elements 4 and 3
8 1 4 # edge number 8 is sharing by elements 1 and 4
For both of these structures, struct1.dat and struct2.dat, my code is very slow because I used a brute force approach with a lot of loops..
I am looking for an algorithm (a paper, or better: a subroutine in fortran available for download) optimized for this? I want to continue using triangle and tetgen, but I am willing to listen to other options.

How to find maximum distance apart of values within a variable

I create a working example dataset:
input ///
group value
1 3
1 2
1 3
2 4
2 6
2 7
3 4
3 4
3 4
3 4
4 17
4 2
5 3
5 5
5 12
end
My goal is to figure out the maximum distance between incremental values within group. For group 2, this would be 2, because the next highest value after 4 is 6. Note that the only value relevant to 4 is 6, not 7, because 7 is not the next highest value after 4. The result for group 3 is 0 because there is only one value in group 3. There will only be one result per group.
What I want to get:
input ///
group value result
1 3 1
1 2 1
1 3 1
2 4 2
2 6 2
2 7 2
3 4 0
3 4 0
3 4 0
3 4 0
4 17 15
4 2 15
5 3 7
5 5 7
5 12 7
end
The order is not important, so the order just above can change with no problem.
Any tips?
I may have figured it out:
bys group (value): gen d = value[_n+1] - value[_n]
bys group: egen result = max(d)
drop d

pandas - Perform computation against a reference record within groups

For each row of data in a DataFrame I would like to compute the number of unique values in columns A and B for that particular row and a reference row within the group identified by another column ID. Here is a toy dataset:
d = {'ID' : pd.Series([1,1,1,2,2,2,2,3,3])
,'A' : pd.Series([1,2,3,4,5,6,7,8,9])
,'B' : pd.Series([1,2,3,4,11,12,13,14,15])
,'REFERENCE' : pd.Series([1,0,0,0,0,1,0,1,0])}
data = pd.DataFrame(d)
The data looks like this:
In [3]: data
Out[3]:
A B ID REFERENCE
0 1 1 1 1
1 2 2 1 0
2 3 3 1 0
3 4 4 2 0
4 5 11 2 0
5 6 12 2 1
6 7 13 2 0
7 8 14 3 1
8 9 15 3 0
Now, within each group defined using ID I want to compare each record with the reference record and I want to compute the number of unique A and B values for the combination. For instance, I can compute the value for data record 3 by taking len(set([4,4,6,12])) which gives 3. The result should look like this:
A B ID REFERENCE CARDINALITY
0 1 1 1 1 1
1 2 2 1 0 2
2 3 3 1 0 2
3 4 4 2 0 3
4 5 11 2 0 4
5 6 12 2 1 2
6 7 13 2 0 4
7 8 14 3 1 2
8 9 15 3 0 3
The only way I can think of implementing this is using for loops that loop over each grouped object and then each record within the grouped object and computes it against the reference record. This is non-pythonic and very slow. Can anyone please suggest a vectorized approach to achieve the same?
I would create a new column where I combine a and b into a tuple and then I would group by And then use groups = dict(list(groupby)) and then get the length of each frame using len()

Function that given these 2 values produces this third value?

I'm trying to write a function that when given 2 arguments, the 2 leftmost columns, produces the third column as a result:
0 0 0
1 0 3
2 0 2
3 0 1
0 1 1
1 1 0
2 1 3
3 1 2
0 2 2
1 2 1
2 2 0
3 2 3
0 3 3
1 3 2
2 3 1
3 3 0
I know there will be a modulus involved but I can't quite figure it out.
I'm trying to figure out if 4 people are sitting at a table, given the person and target, from the person's perspective which seat is the target sitting in?
Thanks
If a and b are the positions of the two persons, their "distance" is:
(4+b-a) % 4
This also shows that the forth block in your example is wrong.
Assuming that last block of numbers is wrong, I think you're looking for (4 + b - a) % 4 gives c (for columns a b c).