simple code with rpc/rget/rput

Issue #232 closed
Ngoc Phuong Chau created an issue

I want to test a array with 10 elements. I want to receive a new array with double values. first = [0,1,2,3,4,5,6,7,8,9]

The final values is: 0 2 4 6 8 10 12 14 16 18

#include <upcxx/upcxx.hpp>
#include <iostream>

using namespace std;


int double_num(int num)
{
        cout<<"process: "<<upcxx::rank_me()<<endl;
        return num*2;
}

int main(int argc, char **argv)
{
        upcxx::global_ptr<int> first =upcxx::new_<int>(10);
        int*x = first.local();

        for(int i=0; i<10; i++)
        {
                x[i]=i;
        }

    upcxx::init(); //start upc++

        upcxx::future<int> f1= upcxx::rget(first);
        upcxx::future<int> f2= f1.then(double_num);

        f2.wait();

        upcxx::finalize(); //end upc++

        cout<<"Results: "<<endl;

        x = first.local();
        for(int i=0; i<10; i++)
        {
                cout << x[i]<<" ";
        }


    upcxx::delete_(first);
        return 0;
}

Results: Segmentation fault

Please help me to figure out how to change in this code?

Thanks,

Comments (4)

  1. Dan Bonachea

    Hi Ngoc-

    upcxx::new_ and upcxx::delete_ must be called while the library is initialized, between init and finalize, which is likely the cause of your crash. Also, I highly recommend developing in debug mode (upcxx -g and/or UPCXX_CODEMODE=debug), which enables assertion checks for many such mistakes.

    Once that's fixed, I don't think this program does what you expect. First, there is no real communication in this program. Every rank is allocating its own array and then reading a copy of the first element, and then doubling that value, which is placed into f2.result(). However nothing after the initialization loop is writing to the array, so there's no reason to expect those values to change.

    I highly recommend reading the Programmer's Guide for an introduction to UPC++ programming.

    Hope this helps...

  2. Log in to comment