How do I use MPI to parallelize the maximum subarray problem?

Viewed 47

The maximum subarray problem consists of found the subarray that has the maximum summation inside an array.

Suppose I have A = [-2,1,-3,4,-1,2,1-5,4], then the maximum subarray is [4,-1,2,1], wich summation is 6.

The code bellow is the sequential answer for the max subarray problem

#include<iostream> 
#include<climits> 
using namespace std; 

int main() 
{ 
    int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; 
    int size = sizeof(a)/sizeof(a[0]); 
    int max_so_far = INT_MIN, max_ending_here = 0; 

    for (int i = 0; i < size; i++) 
    { 
        max_ending_here = max_ending_here + a[i]; 
        if (max_so_far < max_ending_here) 
            max_so_far = max_ending_here; 

        if (max_ending_here < 0) 
            max_ending_here = 0; 
    } 
    cout << "Maximum contiguous sum is " << max_so_far; 
    return 0; 
} 

How can I parallelize this code using MPI?

Thanks for any help!

0 Answers
Related