Parallel program finds subscript with minimum number of columns

Source: Internet
Author: User
Tags array length arrays

Using MPI to design a parallel program to find the smallest element value in a sequence or multidimensional array, I believe that anyone who has a parallel programming will. But if you need to determine the subscript of the minimum element value, it's a little tricky.

Take one-dimensional arrays as an example.

Originally I used a rather stupid method, that is, in the root process to create two length of the process number of a one-dimensional array, respectively, to store the collection operation from all processes collected by the local minimum element value and its corresponding subscript, and then in the root process through the one-dimensional array to find all the minimum value serially, The corresponding other one-dimensional array will be able to get the small mark.

For example: Minidistance, Miniindex are two one-dimensional arrays that store the minimum element value and corresponding subscript, and their values are obtained by collecting local minimum values through MPI_GATHERV operations. If the root process gets minidistance[i] is the global minimum, then the corresponding miniindex[i] is the corresponding subscript for the global minimum value. The disadvantage of this method is obvious, requires two collection operations, the root process to find the global minimum value, the process is many, parallel programs will be very low.

To improve efficiency, you can create a struct variable that contains two elements, a stored element value, and a storage corresponding subscript so that only one collection operation is required to achieve the goal. However, in addition to the need to construct a new MPI data type in order to send and receive structural data, it is still necessary to do a serial one-dimensional lookup of the global minimum at the root process, without fundamentally solving the problem.

Today, when testing the program, it suddenly occurred to be possible to encode the subscript and the element values together to form another value, while maintaining the original element value of the relative size of the same, and then decode the subscript after the return. Tried it, the result was very successful. As an example:

A one-dimensional array of length 100000, whose element values range from 1-100, then, after each process locally using the serial algorithm to find the local minimum value, multiply the minimum value by 100000, and then add the lowest value subscript, get the new value, and then the root process to the new value, By taking the resulting global minimum value to 100000 modulo, we can get the global minimum value of the subscript.

Let's analyze why the above approach works. Because the array length is 100000, the subscript value range is 0-99999. Instead of multiplying the element value by 100000, at least greater than or equal to 100000, plus a subscript value less than 100000, the original element value remains relatively large. And after the root process to 100000 modulo, it is obvious that the previous addition to the lower than 100000 of the subscript value. Using this method is OK with a single-pass operation, no longer requires additional operations by the root process (except for a single modulo operation). The efficiency gains are obvious.

The use of this method to pay attention to the range of element values, to ensure that the subscript value on the relative size of the element value has no effect, and to ensure that the new value after the operation does not exceed the value of the data type range.

Later found another way to solve the problem. Use the Mpi_allreduce function. The function can mpi_reduce a one-dimensional array of only two elements, as if it were about a single element, and each process is given a normalized result after the function has been normalized, eliminating the need for additional broadcasts. We know the OP parameter in the function, which indicates what sort of operation to take, and there are multiple actions to choose from. If you select Mpi_minloc, the first element of the array of two elements to be normalized is minimized, that is, the first element is compared to the minimum value, and if the first element of the array with multiple processes is equal, then the two elements are compared and the minimum value is taken. So the mpi_minloc operation is called the min-min operation.

Thus, if the first element of the array holds the value of the element, and the second element holds the subscript for the corresponding element value, then it is entirely possible to achieve our goal with a single-step induction operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.