<commit_before>// Version: $Id$
// 
// 

// Commentary: 
// 
// 

// Change Log:
// 
// 

// Code:

#pragma once

#include "dtkDistributed.h"
#include "dtkDistributedCommunicator.h"
#include "dtkDistributedMapper.h"
#include "dtkDistributedBufferManager.h"

// ///////////////////////////////////////////////////////////////////
// dtkDistributedArray private implementation
// ///////////////////////////////////////////////////////////////////

template <typename T> inline void dtkDistributedArray<T>::allocate(dtkDistributedBufferManager *& manager, Data *& x, qlonglong size)
{
    if (size > 0) {
        manager = m_comm->createBufferManager();
        x = dtkTypedArrayData<T>::fromRawData(manager->allocate<T>(size), size, dtkArrayData::RawData);
    }
}

template <typename T> inline void dtkDistributedArray<T>::deallocate(dtkDistributedBufferManager *manager, Data *x)
{
    if (!x)
        return;

    if (!x->ref.deref()) {
        if (QTypeInfo<T>::isComplex) {
            T *from = x->begin();
            T *to   = x->end();
            while (from != to) {
                from++->~T();
            }
        }
        T *buffer = static_cast<T *>(x->data());
        manager->deallocate(buffer);
        free(x);
        m_comm->destroyBufferManager(manager);
    }
}

template <typename T> inline void dtkDistributedArray<T>::copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom)
{
    if (QTypeInfo<T>::isComplex) {
        while (srcFrom != srcTo)
            new (dstFrom++) T(*srcFrom++);
    } else {
        ::memcpy(static_cast<void *>(dstFrom), static_cast<const void *>(srcFrom), (srcTo - srcFrom) * sizeof(T));
    }
}

// ///////////////////////////////////////////////////////////////////
// dtkDistributedArray implementation
// ///////////////////////////////////////////////////////////////////

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size) : dtkDistributedContainer(size),
                                                                                                  d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    firstIndex = m_mapper->firstIndex(this->wid());
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size, dtkDistributedMapper *mapper) : dtkDistributedContainer(size, mapper),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    if (m_mapper->count() == 0)
        m_mapper->setMapping(size, m_comm->size());

    firstIndex = m_mapper->firstIndex(this->wid());
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size, const T& init_value) : dtkDistributedContainer(size),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    firstIndex = m_mapper->firstIndex(this->wid());
    this->fill(init_value);
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size, const T *array) : dtkDistributedContainer(size),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    if (array) {
        for (qlonglong i = 0; i < d->size; ++i) {
            d->begin()[i] = array[m_mapper->localToGlobal(i, this->wid())];
        }
    }
    firstIndex = m_mapper->firstIndex(this->wid());
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const dtkArray<T>& array) : dtkDistributedContainer(array.size()),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    for (qlonglong i = 0; i < d->size; ++i) {
        d->begin()[i] = array[m_mapper->localToGlobal(i, this->wid())];
    }
    firstIndex = m_mapper->firstIndex(this->wid());
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const dtkDistributedArray& o) : dtkDistributedContainer(o.size(), o.mapper()),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    if (d) {

        typename Data::iterator oit  = o.d->begin();
        typename Data::iterator oend = o.d->end();
        typename Data::iterator it  = o.d->begin();
        for(; oit != oend; ++oit) {
            *it = *oit;
            it++;
        }
    }
    firstIndex = m_mapper->firstIndex(this->wid());
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::~dtkDistributedArray(void)
{
    m_comm->barrier();
    this->deallocate(m_buffer_manager, d);
    d = 0;
    m_buffer_manager = 0;
<commit_msg>fix constructor<commit_after>// Version: $Id$
// 
// 

// Commentary: 
// 
// 

// Change Log:
// 
// 

// Code:

#pragma once

#include "dtkDistributed.h"
#include "dtkDistributedCommunicator.h"
#include "dtkDistributedMapper.h"
#include "dtkDistributedBufferManager.h"

// ///////////////////////////////////////////////////////////////////
// dtkDistributedArray private implementation
// ///////////////////////////////////////////////////////////////////

template <typename T> inline void dtkDistributedArray<T>::allocate(dtkDistributedBufferManager *& manager, Data *& x, qlonglong size)
{
    if (size > 0) {
        manager = m_comm->createBufferManager();
        x = dtkTypedArrayData<T>::fromRawData(manager->allocate<T>(size), size, dtkArrayData::RawData);
    } else {
        qWarning() << "allocation with size =0!" << m_comm->wid();
    }
}

template <typename T> inline void dtkDistributedArray<T>::deallocate(dtkDistributedBufferManager *manager, Data *x)
{
    if (!x)
        return;

    if (!x->ref.deref()) {
        if (QTypeInfo<T>::isComplex) {
            T *from = x->begin();
            T *to   = x->end();
            while (from != to) {
                from++->~T();
            }
        }
        T *buffer = static_cast<T *>(x->data());
        manager->deallocate(buffer);
        free(x);
        m_comm->destroyBufferManager(manager);
    }
}

template <typename T> inline void dtkDistributedArray<T>::copyConstruct(const T *srcFrom, const T *srcTo, T *dstFrom)
{
    if (QTypeInfo<T>::isComplex) {
        while (srcFrom != srcTo)
            new (dstFrom++) T(*srcFrom++);
    } else {
        ::memcpy(static_cast<void *>(dstFrom), static_cast<const void *>(srcFrom), (srcTo - srcFrom) * sizeof(T));
    }
}

// ///////////////////////////////////////////////////////////////////
// dtkDistributedArray implementation
// ///////////////////////////////////////////////////////////////////

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size) : dtkDistributedContainer(size),
                                                                                                  d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    firstIndex = m_mapper->firstIndex(this->wid());
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size, dtkDistributedMapper *mapper) : dtkDistributedContainer(size, mapper),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    if (m_mapper->count() == 0)
        m_mapper->setMapping(size, m_comm->size());

    firstIndex = m_mapper->firstIndex(this->wid());
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size, const T& init_value) : dtkDistributedContainer(size),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    firstIndex = m_mapper->firstIndex(this->wid());
    this->fill(init_value);
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const qlonglong& size, const T *array) : dtkDistributedContainer(size),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    if (array) {
        for (qlonglong i = 0; i < d->size; ++i) {
            d->begin()[i] = array[m_mapper->localToGlobal(i, this->wid())];
        }
    }
    firstIndex = m_mapper->firstIndex(this->wid());
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const dtkArray<T>& array) : dtkDistributedContainer(array.size()),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    for (qlonglong i = 0; i < d->size; ++i) {
        d->begin()[i] = array[m_mapper->localToGlobal(i, this->wid())];
    }
    firstIndex = m_mapper->firstIndex(this->wid());
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::dtkDistributedArray(const dtkDistributedArray& o) : dtkDistributedContainer(o.size(), o.mapper()),
    d(0), m_cache(new dtkDistributedArrayCache<T>(this)), m_buffer_manager(0)
{
    this->allocate(m_buffer_manager, d, m_mapper->count(this->wid()));

    if (d) {

        typename Data::iterator oit  = o.d->begin();
        typename Data::iterator oend = o.d->end();
        typename Data::iterator it   = d->begin();
        for(; oit != oend; ++oit) {
            *it = *oit;
            it++;
        }
    }
    firstIndex = m_mapper->firstIndex(this->wid());
    m_comm->barrier();
}

template <typename T> inline dtkDistributedArray<T>::~dtkDistributedArray(void)
{
    m_comm->barrier();
    this->deallocate(m_buffer_manager, d);
    d = 0;
    m_buffer_manager = 0;

{'fraction_non_alphanumeric': 0.20652066197601823, 'fraction_numerical': 0.003666633633931226, 'mean_word_length': 2.782608695652174, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 194, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5925571', 'n_tokens_mistral': 3274, 'n_tokens_neox': 3199, 'n_words': 572}