#ifndef __X10_UTIL_CUDAUTILITIES_H
#define __X10_UTIL_CUDAUTILITIES_H

#include <x10rt.h>


#define X10_LANG_OBJECT_H_NODEPS
#include <x10/lang/Object.h>
#undef X10_LANG_OBJECT_H_NODEPS
namespace x10 { namespace lang { 
class Int;
} } 
namespace x10 { namespace util { 
template<class TPMGL(T)> class IndexedMemoryChunk;
} } 
namespace x10 { namespace util { 
template<class TPMGL(T)> class RemoteIndexedMemoryChunk;
} } 
namespace x10 { namespace lang { 
class Runtime;
} } 
namespace x10 { namespace lang { 
class FinishState;
} } 
namespace x10 { namespace lang { 
class Throwable;
} } 
namespace x10 { namespace lang { 
class RuntimeException;
} } 
namespace x10 { namespace compiler { 
class Finalization;
} } 
namespace x10 { namespace compiler { 
class Abort;
} } 
namespace x10 { namespace compiler { 
class CompilerFlags;
} } 
namespace x10 { namespace lang { 
class Boolean;
} } 
namespace x10 { namespace lang { 
class Place;
} } 
namespace x10 { namespace array { 
template<class TPMGL(T)> class RemoteArray;
} } 
namespace x10 { namespace array { 
class Region;
} } 
namespace x10 { namespace compiler { 
class Native;
} } 
namespace x10 { namespace lang { 
class UnsupportedOperationException;
} } 
namespace x10 { namespace array { 
template<class TPMGL(T)> class Array;
} } 
namespace x10 { namespace lang { 
template<class TPMGL(U)> class Fun_0_0;
} } 
namespace x10 { namespace lang { 
template<class TPMGL(Z1), class TPMGL(U)> class Fun_0_1;
} } 
namespace x10 { namespace lang { 
template<class TPMGL(T)> class GlobalRef;
} } 
namespace x10 { namespace lang { 
class FailedDynamicCheckException;
} } 
namespace x10 { namespace lang { 
class Zero;
} } 
namespace x10 { namespace util { 

class CUDAUtilities : public x10::lang::Object   {
    public:
    RTT_H_DECLS_CLASS
    
    static x10_int autoBlocks();
    static x10_int autoThreads();
    template<class TPMGL(T)> static void initCUDAArray(x10::util::IndexedMemoryChunk<TPMGL(T) > local,
                                                       x10::util::RemoteIndexedMemoryChunk<TPMGL(T) > remote,
                                                       x10_int numElements);
    template<class TPMGL(T)> static x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
      makeCUDAArray(x10::lang::Place gpu, x10_int numElements,
                    x10::util::IndexedMemoryChunk<TPMGL(T) > init);
    template<class TPMGL(T)> static x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
      makeRemoteArray(x10::lang::Place place, x10_int numElements,
                      x10aux::ref<x10::array::Array<TPMGL(T)> > init);
    template<class TPMGL(T)> static x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
      makeRemoteArray(x10::lang::Place place, x10_int numElements);
    template<class TPMGL(T)> static x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
      makeRemoteArray(x10::lang::Place place, x10_int numElements,
                      TPMGL(T) init);
    template<class TPMGL(T)> static x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
      makeRemoteArray(x10::lang::Place place, x10_int numElements,
                      x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > init);
    template<class TPMGL(T)> static void deleteRemoteArray(
      x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > arr);
    static x10_int mul24(x10_int a, x10_int b);
    virtual x10aux::ref<x10::util::CUDAUtilities> x10__util__CUDAUtilities____x10__util__CUDAUtilities__this(
      );
    void _constructor();
    
    static x10aux::ref<x10::util::CUDAUtilities> _make();
    
    
    // Serialization
    public: static const x10aux::serialization_id_t _serialization_id;
    
    public: virtual x10aux::serialization_id_t _get_serialization_id() {
         return _serialization_id;
    }
    
    public: virtual void _serialize_body(x10aux::serialization_buffer& buf);
    
    public: static x10aux::ref<x10::lang::Reference> _deserializer(x10aux::deserialization_buffer& buf);
    
    public: void _deserialize_body(x10aux::deserialization_buffer& buf);
    
};

} } 
#endif // X10_UTIL_CUDAUTILITIES_H

namespace x10 { namespace util { 
class CUDAUtilities;
} } 

#ifndef X10_UTIL_CUDAUTILITIES_H_NODEPS
#define X10_UTIL_CUDAUTILITIES_H_NODEPS
#include <x10/lang/Object.h>
#include <x10/lang/Int.h>
#include <x10/util/IndexedMemoryChunk.h>
#include <x10/util/RemoteIndexedMemoryChunk.h>
#include <x10/lang/Runtime.h>
#include <x10/lang/FinishState.h>
#include <x10/lang/Throwable.h>
#include <x10/lang/RuntimeException.h>
#include <x10/compiler/Finalization.h>
#include <x10/compiler/Abort.h>
#include <x10/compiler/CompilerFlags.h>
#include <x10/lang/Boolean.h>
#include <x10/lang/Place.h>
#include <x10/array/RemoteArray.h>
#include <x10/array/Region.h>
#include <x10/compiler/Native.h>
#include <x10/lang/UnsupportedOperationException.h>
#include <x10/array/Array.h>
#include <x10/lang/Fun_0_0.h>
#include <x10/lang/Fun_0_1.h>
#include <x10/lang/GlobalRef.h>
#include <x10/lang/FailedDynamicCheckException.h>
#include <x10/lang/Zero.h>
#ifndef X10_UTIL_CUDAUTILITIES__CLOSURE__2_CLOSURE
#define X10_UTIL_CUDAUTILITIES__CLOSURE__2_CLOSURE
#include <x10/lang/Closure.h>
#include <x10/lang/Fun_0_1.h>
template<class TPMGL(T)> class x10_util_CUDAUtilities__closure__2 : public x10::lang::Closure {
    public:
    
    static typename x10::lang::Fun_0_1<x10_int, TPMGL(T)>::template itable <x10_util_CUDAUtilities__closure__2<TPMGL(T) > > _itable;
    static x10aux::itable_entry _itables[2];
    
    virtual x10aux::itable_entry* _getITables() { return _itables; }
    
    // closure body
    TPMGL(T) __apply(x10_int p) {
        
        //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10aux::nullCheck(init)->x10::array::Array<TPMGL(T)>::__apply(
                 p);
        
    }
    
    // captured environment
    x10aux::ref<x10::array::Array<TPMGL(T)> > init;
    
    x10aux::serialization_id_t _get_serialization_id() {
        return _serialization_id;
    }
    
    void _serialize_body(x10aux::serialization_buffer &buf) {
        buf.write(this->init);
    }
    
    template<class __T> static x10aux::ref<__T> _deserialize(x10aux::deserialization_buffer &buf) {
        x10_util_CUDAUtilities__closure__2<TPMGL(T) >* storage = x10aux::alloc<x10_util_CUDAUtilities__closure__2<TPMGL(T) > >();
        buf.record_reference(x10aux::ref<x10_util_CUDAUtilities__closure__2<TPMGL(T) > >(storage));
        x10aux::ref<x10::array::Array<TPMGL(T)> > that_init = buf.read<x10aux::ref<x10::array::Array<TPMGL(T)> > >();
        x10aux::ref<x10_util_CUDAUtilities__closure__2<TPMGL(T) > > this_ = new (storage) x10_util_CUDAUtilities__closure__2<TPMGL(T) >(that_init);
        return this_;
    }
    
    x10_util_CUDAUtilities__closure__2(x10aux::ref<x10::array::Array<TPMGL(T)> > init) : init(init) { }
    
    static const x10aux::serialization_id_t _serialization_id;
    
    static const x10aux::RuntimeType* getRTT() { return x10aux::getRTT<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(); }
    virtual const x10aux::RuntimeType *_type() const { return x10aux::getRTT<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(); }
    
    x10aux::ref<x10::lang::String> toString() {
        return x10aux::string_utils::lit(this->toNativeString());
    }
    
    const char* toNativeString() {
        return "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10:67";
    }

};

template<class TPMGL(T)> typename x10::lang::Fun_0_1<x10_int, TPMGL(T)>::template itable <x10_util_CUDAUtilities__closure__2<TPMGL(T) > >x10_util_CUDAUtilities__closure__2<TPMGL(T) >::_itable(&x10::lang::Reference::equals, &x10::lang::Closure::hashCode, &x10_util_CUDAUtilities__closure__2<TPMGL(T) >::__apply, &x10_util_CUDAUtilities__closure__2<TPMGL(T) >::toString, &x10::lang::Closure::typeName);
template<class TPMGL(T)> x10aux::itable_entry x10_util_CUDAUtilities__closure__2<TPMGL(T) >::_itables[2] = {x10aux::itable_entry(&x10aux::getRTT<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >, &x10_util_CUDAUtilities__closure__2<TPMGL(T) >::_itable),x10aux::itable_entry(NULL, NULL)};

template<class TPMGL(T)> const x10aux::serialization_id_t x10_util_CUDAUtilities__closure__2<TPMGL(T) >::_serialization_id = 
    x10aux::DeserializationDispatcher::addDeserializer(x10_util_CUDAUtilities__closure__2<TPMGL(T) >::template _deserialize<x10::lang::Reference>,x10aux::CLOSURE_KIND_NOT_ASYNC);

#endif // X10_UTIL_CUDAUTILITIES__CLOSURE__2_CLOSURE
#ifndef X10_UTIL_CUDAUTILITIES__CLOSURE__1_CLOSURE
#define X10_UTIL_CUDAUTILITIES__CLOSURE__1_CLOSURE
#include <x10/lang/Closure.h>
#include <x10/lang/Fun_0_0.h>
template<class TPMGL(T)> class x10_util_CUDAUtilities__closure__1 : public x10::lang::Closure {
    public:
    
    static typename x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >::template itable <x10_util_CUDAUtilities__closure__1<TPMGL(T) > > _itable;
    static x10aux::itable_entry _itables[2];
    
    virtual x10aux::itable_entry* _getITables() { return _itables; }
    
    // closure body
    x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > __apply() {
        
        //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::array::RemoteArray<TPMGL(T)>::_make(x10::array::Array<TPMGL(T)>::_make(numElements,
                                                                                           x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(x10aux::ref<x10_util_CUDAUtilities__closure__2<TPMGL(T) > >(new (x10aux::alloc<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(sizeof(x10_util_CUDAUtilities__closure__2<TPMGL(T)>)))x10_util_CUDAUtilities__closure__2<TPMGL(T)>(init)))));
        
    }
    
    // captured environment
    x10_int numElements;
    x10aux::ref<x10::array::Array<TPMGL(T)> > init;
    
    x10aux::serialization_id_t _get_serialization_id() {
        return _serialization_id;
    }
    
    void _serialize_body(x10aux::serialization_buffer &buf) {
        buf.write(this->numElements);
        buf.write(this->init);
    }
    
    template<class __T> static x10aux::ref<__T> _deserialize(x10aux::deserialization_buffer &buf) {
        x10_util_CUDAUtilities__closure__1<TPMGL(T) >* storage = x10aux::alloc<x10_util_CUDAUtilities__closure__1<TPMGL(T) > >();
        buf.record_reference(x10aux::ref<x10_util_CUDAUtilities__closure__1<TPMGL(T) > >(storage));
        x10_int that_numElements = buf.read<x10_int>();
        x10aux::ref<x10::array::Array<TPMGL(T)> > that_init = buf.read<x10aux::ref<x10::array::Array<TPMGL(T)> > >();
        x10aux::ref<x10_util_CUDAUtilities__closure__1<TPMGL(T) > > this_ = new (storage) x10_util_CUDAUtilities__closure__1<TPMGL(T) >(that_numElements, that_init);
        return this_;
    }
    
    x10_util_CUDAUtilities__closure__1(x10_int numElements, x10aux::ref<x10::array::Array<TPMGL(T)> > init) : numElements(numElements), init(init) { }
    
    static const x10aux::serialization_id_t _serialization_id;
    
    static const x10aux::RuntimeType* getRTT() { return x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(); }
    virtual const x10aux::RuntimeType *_type() const { return x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(); }
    
    x10aux::ref<x10::lang::String> toString() {
        return x10aux::string_utils::lit(this->toNativeString());
    }
    
    const char* toNativeString() {
        return "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10:67";
    }

};

template<class TPMGL(T)> typename x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >::template itable <x10_util_CUDAUtilities__closure__1<TPMGL(T) > >x10_util_CUDAUtilities__closure__1<TPMGL(T) >::_itable(&x10::lang::Reference::equals, &x10::lang::Closure::hashCode, &x10_util_CUDAUtilities__closure__1<TPMGL(T) >::__apply, &x10_util_CUDAUtilities__closure__1<TPMGL(T) >::toString, &x10::lang::Closure::typeName);
template<class TPMGL(T)> x10aux::itable_entry x10_util_CUDAUtilities__closure__1<TPMGL(T) >::_itables[2] = {x10aux::itable_entry(&x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >, &x10_util_CUDAUtilities__closure__1<TPMGL(T) >::_itable),x10aux::itable_entry(NULL, NULL)};

template<class TPMGL(T)> const x10aux::serialization_id_t x10_util_CUDAUtilities__closure__1<TPMGL(T) >::_serialization_id = 
    x10aux::DeserializationDispatcher::addDeserializer(x10_util_CUDAUtilities__closure__1<TPMGL(T) >::template _deserialize<x10::lang::Reference>,x10aux::CLOSURE_KIND_NOT_ASYNC);

#endif // X10_UTIL_CUDAUTILITIES__CLOSURE__1_CLOSURE
#ifndef X10_UTIL_CUDAUTILITIES__CLOSURE__3_CLOSURE
#define X10_UTIL_CUDAUTILITIES__CLOSURE__3_CLOSURE
#include <x10/lang/Closure.h>
#include <x10/lang/Fun_0_0.h>
template<class TPMGL(T)> class x10_util_CUDAUtilities__closure__3 : public x10::lang::Closure {
    public:
    
    static typename x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >::template itable <x10_util_CUDAUtilities__closure__3<TPMGL(T) > > _itable;
    static x10aux::itable_entry _itables[2];
    
    virtual x10aux::itable_entry* _getITables() { return _itables; }
    
    // closure body
    x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > __apply() {
        
        //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::array::RemoteArray<TPMGL(T)>::_make(x10::array::Array<TPMGL(T)>::_make(numElements,
                                                                                           init));
        
    }
    
    // captured environment
    x10_int numElements;
    TPMGL(T) init;
    
    x10aux::serialization_id_t _get_serialization_id() {
        return _serialization_id;
    }
    
    void _serialize_body(x10aux::serialization_buffer &buf) {
        buf.write(this->numElements);
        buf.write(this->init);
    }
    
    template<class __T> static x10aux::ref<__T> _deserialize(x10aux::deserialization_buffer &buf) {
        x10_util_CUDAUtilities__closure__3<TPMGL(T) >* storage = x10aux::alloc<x10_util_CUDAUtilities__closure__3<TPMGL(T) > >();
        buf.record_reference(x10aux::ref<x10_util_CUDAUtilities__closure__3<TPMGL(T) > >(storage));
        x10_int that_numElements = buf.read<x10_int>();
        TPMGL(T) that_init = buf.read<TPMGL(T)>();
        x10aux::ref<x10_util_CUDAUtilities__closure__3<TPMGL(T) > > this_ = new (storage) x10_util_CUDAUtilities__closure__3<TPMGL(T) >(that_numElements, that_init);
        return this_;
    }
    
    x10_util_CUDAUtilities__closure__3(x10_int numElements, TPMGL(T) init) : numElements(numElements), init(init) { }
    
    static const x10aux::serialization_id_t _serialization_id;
    
    static const x10aux::RuntimeType* getRTT() { return x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(); }
    virtual const x10aux::RuntimeType *_type() const { return x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(); }
    
    x10aux::ref<x10::lang::String> toString() {
        return x10aux::string_utils::lit(this->toNativeString());
    }
    
    const char* toNativeString() {
        return "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10:85";
    }

};

template<class TPMGL(T)> typename x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >::template itable <x10_util_CUDAUtilities__closure__3<TPMGL(T) > >x10_util_CUDAUtilities__closure__3<TPMGL(T) >::_itable(&x10::lang::Reference::equals, &x10::lang::Closure::hashCode, &x10_util_CUDAUtilities__closure__3<TPMGL(T) >::__apply, &x10_util_CUDAUtilities__closure__3<TPMGL(T) >::toString, &x10::lang::Closure::typeName);
template<class TPMGL(T)> x10aux::itable_entry x10_util_CUDAUtilities__closure__3<TPMGL(T) >::_itables[2] = {x10aux::itable_entry(&x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >, &x10_util_CUDAUtilities__closure__3<TPMGL(T) >::_itable),x10aux::itable_entry(NULL, NULL)};

template<class TPMGL(T)> const x10aux::serialization_id_t x10_util_CUDAUtilities__closure__3<TPMGL(T) >::_serialization_id = 
    x10aux::DeserializationDispatcher::addDeserializer(x10_util_CUDAUtilities__closure__3<TPMGL(T) >::template _deserialize<x10::lang::Reference>,x10aux::CLOSURE_KIND_NOT_ASYNC);

#endif // X10_UTIL_CUDAUTILITIES__CLOSURE__3_CLOSURE
#ifndef X10_UTIL_CUDAUTILITIES__CLOSURE__5_CLOSURE
#define X10_UTIL_CUDAUTILITIES__CLOSURE__5_CLOSURE
#include <x10/lang/Closure.h>
#include <x10/lang/Fun_0_1.h>
template<class TPMGL(T)> class x10_util_CUDAUtilities__closure__5 : public x10::lang::Closure {
    public:
    
    static typename x10::lang::Fun_0_1<x10_int, TPMGL(T)>::template itable <x10_util_CUDAUtilities__closure__5<TPMGL(T) > > _itable;
    static x10aux::itable_entry _itables[2];
    
    virtual x10aux::itable_entry* _getITables() { return _itables; }
    
    // closure body
    TPMGL(T) __apply(x10_int p) {
        
        //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::lang::Fun_0_1<x10_int, TPMGL(T)>::__apply(x10aux::nullCheck(init), 
          p);
        
    }
    
    // captured environment
    x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > init;
    
    x10aux::serialization_id_t _get_serialization_id() {
        return _serialization_id;
    }
    
    void _serialize_body(x10aux::serialization_buffer &buf) {
        buf.write(this->init);
    }
    
    template<class __T> static x10aux::ref<__T> _deserialize(x10aux::deserialization_buffer &buf) {
        x10_util_CUDAUtilities__closure__5<TPMGL(T) >* storage = x10aux::alloc<x10_util_CUDAUtilities__closure__5<TPMGL(T) > >();
        buf.record_reference(x10aux::ref<x10_util_CUDAUtilities__closure__5<TPMGL(T) > >(storage));
        x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > that_init = buf.read<x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > >();
        x10aux::ref<x10_util_CUDAUtilities__closure__5<TPMGL(T) > > this_ = new (storage) x10_util_CUDAUtilities__closure__5<TPMGL(T) >(that_init);
        return this_;
    }
    
    x10_util_CUDAUtilities__closure__5(x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > init) : init(init) { }
    
    static const x10aux::serialization_id_t _serialization_id;
    
    static const x10aux::RuntimeType* getRTT() { return x10aux::getRTT<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(); }
    virtual const x10aux::RuntimeType *_type() const { return x10aux::getRTT<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(); }
    
    x10aux::ref<x10::lang::String> toString() {
        return x10aux::string_utils::lit(this->toNativeString());
    }
    
    const char* toNativeString() {
        return "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10:97";
    }

};

template<class TPMGL(T)> typename x10::lang::Fun_0_1<x10_int, TPMGL(T)>::template itable <x10_util_CUDAUtilities__closure__5<TPMGL(T) > >x10_util_CUDAUtilities__closure__5<TPMGL(T) >::_itable(&x10::lang::Reference::equals, &x10::lang::Closure::hashCode, &x10_util_CUDAUtilities__closure__5<TPMGL(T) >::__apply, &x10_util_CUDAUtilities__closure__5<TPMGL(T) >::toString, &x10::lang::Closure::typeName);
template<class TPMGL(T)> x10aux::itable_entry x10_util_CUDAUtilities__closure__5<TPMGL(T) >::_itables[2] = {x10aux::itable_entry(&x10aux::getRTT<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >, &x10_util_CUDAUtilities__closure__5<TPMGL(T) >::_itable),x10aux::itable_entry(NULL, NULL)};

template<class TPMGL(T)> const x10aux::serialization_id_t x10_util_CUDAUtilities__closure__5<TPMGL(T) >::_serialization_id = 
    x10aux::DeserializationDispatcher::addDeserializer(x10_util_CUDAUtilities__closure__5<TPMGL(T) >::template _deserialize<x10::lang::Reference>,x10aux::CLOSURE_KIND_NOT_ASYNC);

#endif // X10_UTIL_CUDAUTILITIES__CLOSURE__5_CLOSURE
#ifndef X10_UTIL_CUDAUTILITIES__CLOSURE__4_CLOSURE
#define X10_UTIL_CUDAUTILITIES__CLOSURE__4_CLOSURE
#include <x10/lang/Closure.h>
#include <x10/lang/Fun_0_0.h>
template<class TPMGL(T)> class x10_util_CUDAUtilities__closure__4 : public x10::lang::Closure {
    public:
    
    static typename x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >::template itable <x10_util_CUDAUtilities__closure__4<TPMGL(T) > > _itable;
    static x10aux::itable_entry _itables[2];
    
    virtual x10aux::itable_entry* _getITables() { return _itables; }
    
    // closure body
    x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > __apply() {
        
        //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::array::RemoteArray<TPMGL(T)>::_make(x10::array::Array<TPMGL(T)>::_make(numElements,
                                                                                           x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(x10aux::ref<x10_util_CUDAUtilities__closure__5<TPMGL(T) > >(new (x10aux::alloc<x10::lang::Fun_0_1<x10_int, TPMGL(T)> >(sizeof(x10_util_CUDAUtilities__closure__5<TPMGL(T)>)))x10_util_CUDAUtilities__closure__5<TPMGL(T)>(init)))));
        
    }
    
    // captured environment
    x10_int numElements;
    x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > init;
    
    x10aux::serialization_id_t _get_serialization_id() {
        return _serialization_id;
    }
    
    void _serialize_body(x10aux::serialization_buffer &buf) {
        buf.write(this->numElements);
        buf.write(this->init);
    }
    
    template<class __T> static x10aux::ref<__T> _deserialize(x10aux::deserialization_buffer &buf) {
        x10_util_CUDAUtilities__closure__4<TPMGL(T) >* storage = x10aux::alloc<x10_util_CUDAUtilities__closure__4<TPMGL(T) > >();
        buf.record_reference(x10aux::ref<x10_util_CUDAUtilities__closure__4<TPMGL(T) > >(storage));
        x10_int that_numElements = buf.read<x10_int>();
        x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > that_init = buf.read<x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > >();
        x10aux::ref<x10_util_CUDAUtilities__closure__4<TPMGL(T) > > this_ = new (storage) x10_util_CUDAUtilities__closure__4<TPMGL(T) >(that_numElements, that_init);
        return this_;
    }
    
    x10_util_CUDAUtilities__closure__4(x10_int numElements, x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > init) : numElements(numElements), init(init) { }
    
    static const x10aux::serialization_id_t _serialization_id;
    
    static const x10aux::RuntimeType* getRTT() { return x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(); }
    virtual const x10aux::RuntimeType *_type() const { return x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(); }
    
    x10aux::ref<x10::lang::String> toString() {
        return x10aux::string_utils::lit(this->toNativeString());
    }
    
    const char* toNativeString() {
        return "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10:97";
    }

};

template<class TPMGL(T)> typename x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >::template itable <x10_util_CUDAUtilities__closure__4<TPMGL(T) > >x10_util_CUDAUtilities__closure__4<TPMGL(T) >::_itable(&x10::lang::Reference::equals, &x10::lang::Closure::hashCode, &x10_util_CUDAUtilities__closure__4<TPMGL(T) >::__apply, &x10_util_CUDAUtilities__closure__4<TPMGL(T) >::toString, &x10::lang::Closure::typeName);
template<class TPMGL(T)> x10aux::itable_entry x10_util_CUDAUtilities__closure__4<TPMGL(T) >::_itables[2] = {x10aux::itable_entry(&x10aux::getRTT<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >, &x10_util_CUDAUtilities__closure__4<TPMGL(T) >::_itable),x10aux::itable_entry(NULL, NULL)};

template<class TPMGL(T)> const x10aux::serialization_id_t x10_util_CUDAUtilities__closure__4<TPMGL(T) >::_serialization_id = 
    x10aux::DeserializationDispatcher::addDeserializer(x10_util_CUDAUtilities__closure__4<TPMGL(T) >::template _deserialize<x10::lang::Reference>,x10aux::CLOSURE_KIND_NOT_ASYNC);

#endif // X10_UTIL_CUDAUTILITIES__CLOSURE__4_CLOSURE
#ifndef X10_UTIL_CUDAUTILITIES_H_GENERICS
#define X10_UTIL_CUDAUTILITIES_H_GENERICS
#ifndef X10_UTIL_CUDAUTILITIES_H_initCUDAArray_1651
#define X10_UTIL_CUDAUTILITIES_H_initCUDAArray_1651
template<class TPMGL(T)> void x10::util::CUDAUtilities::initCUDAArray(x10::util::IndexedMemoryChunk<TPMGL(T) > local,
                                                                      x10::util::RemoteIndexedMemoryChunk<TPMGL(T) > remote,
                                                                      x10_int numElements) {
    {
        
        //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
        x10::lang::Runtime::ensureNotInAtomic();
        
        //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10aux::ref<x10::lang::FinishState> x10____var10 =
          x10::lang::Runtime::startFinish();
        {
            
            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::lang::Throwable> throwable46677 =
              X10_NULL;
            
            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Try_c
            try {
                
                //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Try_c
                try {
                    {
                        
                        //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
                        x10::util::IndexedMemoryChunk<void>::asyncCopy<TPMGL(T) >(local,((x10_int)0),remote,((x10_int)0),numElements);
                    }
                }
                catch (x10aux::__ref& __ref__1652) {
                    x10aux::ref<x10::lang::Throwable>& __exc__ref__1652 = (x10aux::ref<x10::lang::Throwable>&)__ref__1652;
                    if (true) {
                        x10aux::ref<x10::lang::Throwable> __lowerer__var__10__ =
                          static_cast<x10aux::ref<x10::lang::Throwable> >(__exc__ref__1652);
                        {
                            
                            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
                            x10::lang::Runtime::pushException(
                              __lowerer__var__10__);
                            
                            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
                            x10aux::throwException(x10aux::nullCheck(x10::lang::RuntimeException::_make()));
                        }
                    } else
                    throw;
                }
                
                //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
                x10::compiler::Finalization::plausibleThrow();
            }
            catch (x10aux::__ref& __ref__1653) {
                x10aux::ref<x10::lang::Throwable>& __exc__ref__1653 = (x10aux::ref<x10::lang::Throwable>&)__ref__1653;
                if (true) {
                    x10aux::ref<x10::lang::Throwable> formal46678 =
                      static_cast<x10aux::ref<x10::lang::Throwable> >(__exc__ref__1653);
                    {
                        
                        //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10LocalAssign_c
                        throwable46677 = formal46678;
                    }
                } else
                throw;
            }
            
            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
            if ((!x10aux::struct_equals(X10_NULL, throwable46677)))
            {
                
                //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
                if (x10aux::instanceof<x10aux::ref<x10::compiler::Abort> >(throwable46677))
                {
                    
                    //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
                    x10aux::throwException(x10aux::nullCheck(throwable46677));
                }
                
            }
            
            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
            if (true) {
                
                //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
                x10::lang::Runtime::stopFinish(x10____var10);
            }
            
            //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
            if ((!x10aux::struct_equals(X10_NULL, throwable46677)))
            {
                
                //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
                if (!(x10aux::instanceof<x10aux::ref<x10::compiler::Finalization> >(throwable46677)))
                {
                    
                    //#line 46 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
                    x10aux::throwException(x10aux::nullCheck(throwable46677));
                }
                
            }
            
        }
    }
}
#endif // X10_UTIL_CUDAUTILITIES_H_initCUDAArray_1651
#ifndef X10_UTIL_CUDAUTILITIES_H_makeCUDAArray_1654
#define X10_UTIL_CUDAUTILITIES_H_makeCUDAArray_1654
template<class TPMGL(T)> x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
  x10::util::CUDAUtilities::makeCUDAArray(x10::lang::Place gpu,
                                          x10_int numElements,
                                          x10::util::IndexedMemoryChunk<TPMGL(T) > init) {
    
    //#line 51 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
    x10aux::ref<x10::array::Region> reg = x10::array::Region::__implicit_convert(
                                            x10::lang::IntRange::_make(((x10_int)0), ((x10_int) ((numElements) - (((x10_int)1))))));
    x10_ulong addr = x10aux::remote_alloc(gpu.FMGL(id), ((size_t)numElements)*sizeof(TPMGL(T)));
RemoteIndexedMemoryChunk<TPMGL(T)> rimc(addr, numElements, gpu);
initCUDAArray<TPMGL(T)>(init,rimc,numElements);
return x10::array::RemoteArray<TPMGL(T)>::_make(reg, rimc);

    
    //#line 58 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
    x10aux::throwException(x10aux::nullCheck(x10::lang::UnsupportedOperationException::_make()));
}
#endif // X10_UTIL_CUDAUTILITIES_H_makeCUDAArray_1654
#ifndef X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1655
#define X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1655
template<class TPMGL(T)> x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
  x10::util::CUDAUtilities::makeRemoteArray(x10::lang::Place place,
                                            x10_int numElements,
                                            x10aux::ref<x10::array::Array<TPMGL(T)> > init) {
    
    //#line 64 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
    if (place->x10::lang::Place::isCUDA()) {
        
        //#line 65 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::util::CUDAUtilities::template makeCUDAArray<TPMGL(T) >(
                 place, numElements, x10aux::nullCheck(init)->x10::array::Array<TPMGL(T)>::raw());
        
    } else {
        
        //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return (__extension__ ({
            
            //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > __desugarer__var__63__46650 =
              x10::lang::Runtime::template evalAt<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >(
                place, x10aux::ref<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(x10aux::ref<x10_util_CUDAUtilities__closure__1<TPMGL(T) > >(new (x10aux::alloc<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(sizeof(x10_util_CUDAUtilities__closure__1<TPMGL(T)>)))x10_util_CUDAUtilities__closure__1<TPMGL(T)>(numElements, init))));
            
            //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > ret46651;
            
            //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
            if (!((x10aux::struct_equals(x10aux::nullCheck(x10aux::nullCheck(__desugarer__var__63__46650)->
                                                             FMGL(region))->
                                           FMGL(rank), ((x10_int)1))) &&
                (x10aux::struct_equals(x10::lang::Place::place((x10aux::nullCheck(__desugarer__var__63__46650)->
                                                                  FMGL(array))->location),
                                       place)))) {
                
                //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
                if (true) {
                    
                    //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
                    x10aux::throwException(x10aux::nullCheck(x10::lang::FailedDynamicCheckException::_make(x10aux::string_utils::lit("x10.array.RemoteArray[T]{self.region.rank==1, self.array.home==place}"))));
                }
                
            }
            
            //#line 67 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10LocalAssign_c
            ret46651 = __desugarer__var__63__46650;
            ret46651;
        }))
        ;
        
    }
    
}
#endif // X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1655
#ifndef X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1656
#define X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1656
template<class TPMGL(T)> x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
  x10::util::CUDAUtilities::makeRemoteArray(x10::lang::Place place,
                                            x10_int numElements) {
    
    //#line 74 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
    return x10::util::CUDAUtilities::template makeRemoteArray<TPMGL(T) >(
             place, numElements, x10aux::zeroValue<TPMGL(T) >());
    
}
#endif // X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1656
#ifndef X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1657
#define X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1657
template<class TPMGL(T)> x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
  x10::util::CUDAUtilities::makeRemoteArray(x10::lang::Place place,
                                            x10_int numElements,
                                            TPMGL(T) init) {
    
    //#line 80 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
    if (place->x10::lang::Place::isCUDA()) {
        
        //#line 81 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10::util::IndexedMemoryChunk<TPMGL(T) > chunk = x10::util::IndexedMemoryChunk<void>::allocate<TPMGL(T) >(numElements, 8, false, false);
        
        //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10aux::ref<x10::array::Region> p46659 = x10::array::Region::__implicit_convert(
                                                   x10::lang::IntRange::_make(((x10_int)0), ((x10_int) ((numElements) - (((x10_int)1))))));
        
        //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10_int i46614min46660 = p46659->min(((x10_int)0));
        
        //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10_int i46614max46661 = p46659->max(((x10_int)0));
        
        //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.For_c
        {
            x10_int i46662;
            for (
                 //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
                 i46662 = i46614min46660; ((i46662) <= (i46614max46661));
                 
                 //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10LocalAssign_c
                 i46662 = ((x10_int) ((i46662) + (((x10_int)1)))))
            {
                
                //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
                x10_int i46663 = i46662;
                
                //#line 82 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
                (chunk)->__set(i46663, init);
            }
        }
        
        //#line 83 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::util::CUDAUtilities::template makeCUDAArray<TPMGL(T) >(
                 place, numElements, chunk);
        
    } else {
        
        //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return (__extension__ ({
            
            //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > __desugarer__var__64__46653 =
              x10::lang::Runtime::template evalAt<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >(
                place, x10aux::ref<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(x10aux::ref<x10_util_CUDAUtilities__closure__3<TPMGL(T) > >(new (x10aux::alloc<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(sizeof(x10_util_CUDAUtilities__closure__3<TPMGL(T)>)))x10_util_CUDAUtilities__closure__3<TPMGL(T)>(numElements, init))));
            
            //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > ret46654;
            
            //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
            if (!((x10aux::struct_equals(x10aux::nullCheck(x10aux::nullCheck(__desugarer__var__64__46653)->
                                                             FMGL(region))->
                                           FMGL(rank), ((x10_int)1))) &&
                (x10aux::struct_equals(x10::lang::Place::place((x10aux::nullCheck(__desugarer__var__64__46653)->
                                                                  FMGL(array))->location),
                                       place)))) {
                
                //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
                if (true) {
                    
                    //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
                    x10aux::throwException(x10aux::nullCheck(x10::lang::FailedDynamicCheckException::_make(x10aux::string_utils::lit("x10.array.RemoteArray[T]{self.region.rank==1, self.array.home==place}"))));
                }
                
            }
            
            //#line 85 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10LocalAssign_c
            ret46654 = __desugarer__var__64__46653;
            ret46654;
        }))
        ;
        
    }
    
}
#endif // X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1657
#ifndef X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1658
#define X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1658
template<class TPMGL(T)> x10aux::ref<x10::array::RemoteArray<TPMGL(T)> >
  x10::util::CUDAUtilities::makeRemoteArray(x10::lang::Place place,
                                            x10_int numElements,
                                            x10aux::ref<x10::lang::Fun_0_1<x10_int, TPMGL(T)> > init) {
    
    //#line 92 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
    if (place->x10::lang::Place::isCUDA()) {
        
        //#line 93 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10::util::IndexedMemoryChunk<TPMGL(T) > chunk = x10::util::IndexedMemoryChunk<void>::allocate<TPMGL(T) >(numElements, 8, false, false);
        
        //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10aux::ref<x10::array::Region> p46664 = x10::array::Region::__implicit_convert(
                                                   x10::lang::IntRange::_make(((x10_int)0), ((x10_int) ((numElements) - (((x10_int)1))))));
        
        //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10_int i46633min46665 = p46664->min(((x10_int)0));
        
        //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
        x10_int i46633max46666 = p46664->max(((x10_int)0));
        
        //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.For_c
        {
            x10_int i46667;
            for (
                 //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
                 i46667 = i46633min46665; ((i46667) <= (i46633max46666));
                 
                 //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10LocalAssign_c
                 i46667 = ((x10_int) ((i46667) + (((x10_int)1)))))
            {
                
                //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
                x10_int i46668 = i46667;
                
                //#line 94 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10Call_c
                (chunk)->__set(i46668, x10::lang::Fun_0_1<x10_int, TPMGL(T)>::__apply(x10aux::nullCheck(init), 
                  i46668));
            }
        }
        
        //#line 95 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return x10::util::CUDAUtilities::template makeCUDAArray<TPMGL(T) >(
                 place, numElements, chunk);
        
    } else {
        
        //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10Return_c
        return (__extension__ ({
            
            //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > __desugarer__var__65__46656 =
              x10::lang::Runtime::template evalAt<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > >(
                place, x10aux::ref<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(x10aux::ref<x10_util_CUDAUtilities__closure__4<TPMGL(T) > >(new (x10aux::alloc<x10::lang::Fun_0_0<x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > > >(sizeof(x10_util_CUDAUtilities__closure__4<TPMGL(T)>)))x10_util_CUDAUtilities__closure__4<TPMGL(T)>(numElements, init))));
            
            //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
            x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > ret46657;
            
            //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
            if (!((x10aux::struct_equals(x10aux::nullCheck(x10aux::nullCheck(__desugarer__var__65__46656)->
                                                             FMGL(region))->
                                           FMGL(rank), ((x10_int)1))) &&
                (x10aux::struct_equals(x10::lang::Place::place((x10aux::nullCheck(__desugarer__var__65__46656)->
                                                                  FMGL(array))->location),
                                       place)))) {
                
                //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
                if (true) {
                    
                    //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": polyglot.ast.Throw_c
                    x10aux::throwException(x10aux::nullCheck(x10::lang::FailedDynamicCheckException::_make(x10aux::string_utils::lit("x10.array.RemoteArray[T]{self.region.rank==1, self.array.home==place}"))));
                }
                
            }
            
            //#line 97 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": Eval of x10.ast.X10LocalAssign_c
            ret46657 = __desugarer__var__65__46656;
            ret46657;
        }))
        ;
        
    }
    
}
#endif // X10_UTIL_CUDAUTILITIES_H_makeRemoteArray_1658
#ifndef X10_UTIL_CUDAUTILITIES_H_deleteRemoteArray_1659
#define X10_UTIL_CUDAUTILITIES_H_deleteRemoteArray_1659
template<class TPMGL(T)> void x10::util::CUDAUtilities::deleteRemoteArray(
  x10aux::ref<x10::array::RemoteArray<TPMGL(T)> > arr) {
    
    //#line 103 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10LocalDecl_c
    x10::lang::Place place = x10aux::nullCheck(arr)->x10::array::RemoteArray<TPMGL(T)>::home();
    
    //#line 104 "/wrkdirs/usr/ports/lang/x10/work/x10.runtime/src-x10/x10/util/CUDAUtilities.x10": x10.ast.X10If_c
    if (place->x10::lang::Place::isCUDA()) {
        RemoteIndexedMemoryChunk<TPMGL(T)> rimc = arr->FMGL(rawData);
x10aux::remote_free(place.FMGL(id), (x10_ulong)(size_t)rimc->data);

    }
    
}
#endif // X10_UTIL_CUDAUTILITIES_H_deleteRemoteArray_1659
#endif // X10_UTIL_CUDAUTILITIES_H_GENERICS
#endif // __X10_UTIL_CUDAUTILITIES_H_NODEPS
