MPI_REDUCE on parts of array - fortran

I have arrays defined in Fortran as follows:
integer,dimension(100)::a
integer,dimension(100)::partial_sum_a
I wanted to use MPI_REDUCE to sum only the values of a from indices 5 to 10 (i.e. a(5),...,a(10)) on root. How would I do that? Will the usage of:
MPI_Reduce(a(5:),partial_sum_a(5:),6,...)
be fine? Or do I have to use MPI_TYPE_VECTOR?

Yes, given that an array slice with more than one element is an array as well, the usual usage of MPI_Reduce will work. Obviously, you need to make sure that all the arguments in the MPI_Reduce call are correct, i.e. count matching the number of elements in send buffer etc. Most often you can try these things yourself faster than it takes you to get an answer from people on the internet.

Related

"Tricks" to filling a "rolling" time-series array absent brute-force pushback of all values each iteration

My applications is financial, in C++, Visual Studio 2003.
I'm trying to maintain an array of the last (x) values for an observation, and as each new value arrives I have a loop first push all of the other values back then add the new value in the front.
It's computationally intensive, and I've been trying to be clever and come up with some way around this. I've probably either stated an oxymoronic problem or reached the limit of my intellect, or both.
Here's an idea I have:
Suppose it's 60 seconds of data, and new data arrive each second. Suppose we have an integer between 0 and 59, that will serve to index an element of the array. Suppose each second, when the data arrives, we first iterate the integer then overwrite the element of the array at that index with the new data. Then, suppose in our calculations, we refer to the same integer as the base, work backwards to zero, then to 59 then back down again. The formulas in the math would be a bit more tedious to write. But my application does a lot of these pushback/fills of arrays, each second for several data points, and each array having 3600 elements per data series (one hour of seconds).
Does the board think this is a good idea? Or am I being silly here?
What you're describing is nothing more than a circular buffer.
There's an implementation in Boost, and probably in other
libraries as well, and a good algorithm description on the
Wikipedia (http://en.wikipedia.org/wiki/Circular_buffer).
And yes, it's a good solution for the problem you describe.
You could use modulo as you suggested (hint: x % y is the syntax for "x" modulo "y"), or you could maintain two buffers where you essentially swap which one is the current data to be read and which buffer is the stale data that is to be overwritten. For copying large quantities of plain-old-data (POD) data, you should take a look at memcpy. Of course, in quantitative trading, people will do all sorts of things to get a speed edge, including custom hardware that allows one to bypass several layers of copying.
Are you sure you are talking about arrays? They don't have a "push" operation - it sounds more like an std::vector.
Anyway, here is the solution for what I think that you want:
If I understood it right, you want a collection of 3600 elements and each second the last element drops off and a new element is added.
So you should use a linked list queue for that task. Operations are performed in O(1).

Allocating memory for stock-like adding integers

I have two knows arrays - each array's element contains a digit, all of them become a number. I have to add these two numbers and create outcome array in the same design.
Now, how do I add these numbers? Well, just like in a stack, I take first elements from these two arrays, add them, and save into the, let's say - outcome[0].
Then I take the next first elements of these two arrays (I deleted the previous ones - took it out from the stack), add them, and put into the top of the outcome array.
If an overflow happend (x > 9), I erase the last digit, and put it on the top.
This way I should get the proper results.
After I explained you my cool project, it's time to get to the actual question - how could I allocate the memory for the outcome array?
I mean, I don't really know how many overflows there will be (== how many times will I have to enlarge outcomes array), I don't know if the result of adding two XX numbers will result in XXX or XX number, etc...
I've been thinking about this for a long time and can't come up with anything. Well, maybe except for allocating something like sizeof(int)*999 to make 100% sure :D...
Since you have tagged this c++, I would suggest std::vector and std::stack.
The number of overflows will be at most the number of digits you have in your arrays. So, for c, I would say allocate the maximum of the size of array1 and array2 plus 1.

Bitshifting elements in an array

I have an assignment in which I must read a list of 4000 names from a text file and sort then into a C style array as they're being read in (rather than reading them all then sorting). Since this is going involve a lot elements changing indices would it be possible to use bitshifting to rearrange large quantities of elements simultaneously?For example,
declare a heap based array of 20 size
place variable x index 10
perform a bitshift on index 9 with the size of the array data type so that x is now in index 11
Also, if you have any tips on the task in general I'd appreciate it.
No, that doesn't sound at all like something you'd use bitshifting for.
You will have distinct elements (the names) stored in an array, and you need to change the order of entire elements. This is not what bitshifting is used for; it is used to move the bits in a single integer to the left or to the right.
You should just learn qsort().
Not sure about the "sort as they're being read in" requirement, but the easiest solution would be to just call qsort() as each name is added. If that's not allowed or deemed too expensive, think about how to do a "sorted insert" against an array.
By the way, a typical approach in C would be to work with an array of pointers to strings, rather than an array of actual strings. This is good, since sorting an array of pointers is much easier.
So you would have:
char *names[4000];
instead of
char names[4000][64 /* or whatever */];
This would require you to dynamically allocate space for each name as it's loaded though, which isn't to hard. Especially not if you have strdup(). :)
If using qsort() is not allowed(would be pretty stupid to do so after every insert), you could write your own insertion sort. It's not exactly a very efficient way of sorting large arrays but I suppose it's what your teacher is expecting for.

Can I check in C(++) if an array is all 0 (or false)?

Can I check in C(++) if an array is all 0 (or false) without iterating/looping over every single value and without allocating a new array of the same size (to use memcmp)?
I'm abusing an array of bools to have arbitrary large bitsets at runtime and do some bitflipping on it
You can use the following condition:
(myvector.end() == std::find(myvector.begin(), myvector.end(), true))
Obviously, internally, this loops over all values.
The alternative (which really should avoid looping) is to override all write-access functions, and keep track of whether true has ever been written to your vector.
UPDATE
Lie Ryan's comments below describe a more robust method of doing this, based on the same principle.
If it's not sorted, no. How would you plan on accomplishing that? You would need to inspect every element to see if it's 0 or not! memcmp, of course, would also check every element. It would just be much more expensive since it reads another array as well.
Of course, you can early-out as soon as you hit a non-0 element.
Your only option would be to use SIMD (which technically still checks every element, but using fewer instructions), but you generally don't do that in a generic array.
(Btw, my answer assumes that you have a simple static C/C++ array. If you can specify what kind of array you have, we could be more specific.)
If you know that this is going to be a requirement, you could build a data structure consisting of an array (possibly dynamic) and a count or currently non-zero cells. Obviously the setting of cells must be abstracted through, but that is natural in c++ with overloading, and you can use an opaque type in c.
Assume that you have an array of N element, you can do a bit check against a set of base vectors.
For example, you have a 15-element array you want to test.
You can test it against an 8-element zero array, an 4-element zero array, a 2-element zero array and a 1-element zero array.
You only have to allocate these elements once given that you know the maximum size of arrays you want to test. Furthermore, the test can be done in parallel (and with assembly intrinsic if necessary).
Further improvement in term of memory allocation can be done with using only an 8-element array since a 4-element zero array is simply the first half of the 8-element zero array.
Consider using boost::dynamic_bitset instead. It has a none member and several other std::bitset-like operations, but its length can be set at runtime.
No, you can compare arrays with memcmp, but you can't compare one value against a block of memory.
What you can do is use algorithms in C++ but that still involves a loop internally.
You don't have to iterate over the entire thing, just stop looping on the first non-zero value.
I can't think of any way to check a set of values other than inspecting them each in turn - you could play games with checking the underlying memory as something larger than bool (__int64 say) but alignment is then an issue.
EDIT:
You could keep a separate count of set bits, and check that is non-zero. You'd have to be careful about maintenance of this, so that setting a set bit did not ++ it and so on.
knittl,
I don't suppose you have access to some fancy DMA hardware on the target computer? Sometimes DMA hardware supports exactly the operation you require, i.e. "Is this region of memory all-zero?" This sort of hardware-accelerated comparison is a common solution when dealing with large bit-buffers. For example, some RAID controllers use this mechanism for parity checking.

Subset Problem -- Any Materials?

Yes this is a homework/lab assignment.
I am interesting in coming up with/finding an algorithm (I can comprehend :P) for using "backtracking" to solve the subset sum problem.
Anyone have some helpful resources? I've spent the last hour or so Googling with not much like finding something I think I could actually use. xD
Thanks SO!
Put the data in a vector.
Then write a routine that has 3 arguments: the vector, an index, and a sum.
Call this routine with the following arguments: the vector, 0, 0.
The routine should do the following tasks:
check if we reached the end of the vector (index==size). If this is the case, we can return immediately.
call itself with arguments: the vector, index+1, sum+vector[index]
(in this case we add the element at the index to the sum and continue with the vector)
call itself with arguments: the vector, index+1, sum
(in this case we don't add the element at the index to the sum, but still continue)
I deliberately left out 2 parts in this algorithm:
first, you should check the sum at some point. If it is zero, then you found a correct subset
second, you should also pass knowledge about which elements you used, so if the sum is zero, you can print out the subset. Consider using an STL::set for this.
Alternatively, you can use the return value of the function to determine whether a correct subset has already been found or not.
The complexity of the algorithm is O(2^N) so it will be very slow for big sets.
Have fun.