<commit_before>#include "sixtracklib/common/track_job_cpu.h"

#include <algorithm>
#include <utility>

#if defined( __cplusplus )
    #include "sixtracklib/common/buffer.hpp"
#endif /* defined( __cplusplus ) */

#include "sixtracklib/common/definitions.h"
#include "sixtracklib/common/buffer.h"
#include "sixtracklib/common/particles.h"
#include "sixtracklib/common/internal/track_job_base.h"
#include "sixtracklib/common/output/output_buffer.h"
#include "sixtracklib/common/output/elem_by_elem_config.h"
#include "sixtracklib/common/track.h"


namespace SIXTRL_CXX_NAMESPACE
{
    TrackJobCpu::TrackJobCpu(
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT particles_buffer,
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT beam_elements_buffer,
        TrackJobCpu::size_type const until_turn,
        TrackJobCpu::size_type const num_elem_by_elem_turns  ) :
        TrackJobBase(),
        m_elem_by_elem_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_beam_monitor_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_particle_block_idx( TrackJobCpu::size_type{ 0 } ),
        m_owns_output_buffer( false )

    {
        using size_t  = TrackJobCpu::size_type;
        using index_t = NS(particle_index_t);

        TrackJobCpu::c_buffer_t* output_buffer =
            ::NS(Buffer_new)( size_t{ 0 } );

        index_t min_turn_id = index_t{ -1 };

        ::NS(Particles)* particles = ::NS(Particles_buffer_get_particles)(
            particles_buffer, this->m_particle_block_idx );

        SIXTRL_ASSERT( particles != nullptr );

        int ret = NS(OutputBuffer_prepare)( beam_elements_buffer,
            output_buffer, particles, num_elem_by_elem_turns,
            &this->m_elem_by_elem_index_offset,
            &this->m_beam_monitor_index_offset,
            &min_turn_id );

        SIXTRL_ASSERT( ret == 0 );

        ret = NS(BeamMonitor_assign_output_buffer_from_offset)(
            beam_elements_buffer, output_buffer, min_turn_id,
            this->m_beam_monitor_index_offset );

        SIXTRL_ASSERT( ret == 0 );

        if( num_elem_by_elem_turns > ( size_t{ 0 } ) )
        {
            ::NS(Particles)* elem_by_elem_particles =
                NS(Particles_buffer_get_particles)(
                    output_buffer, this->m_elem_by_elem_index_offset );

            SIXTRL_ASSERT( elem_by_elem_particles != nullptr );

            ret = NS(Track_all_particles_element_by_element_until_turn)(
                particles, beam_elements_buffer,
                min_turn_id + num_elem_by_elem_turns, elem_by_elem_particles );

            SIXTRL_ASSERT( ret == 0 );
        }

        if( until_turn > size_t{ 0 } )
        {
            ret = NS(Track_all_particles_until_turn)(
                particles, beam_elements_buffer, until_turn );

            SIXTRL_ASSERT( ret == 0 );
        }

        this->doSetPtrToParticlesBuffer( particles_buffer );
        this->doSetPtrToBeamElementsBuffer( beam_elements_buffer );
        this->doSetPtrToOutputBuffer( output_buffer );

        this->m_owns_output_buffer = true;

        ( void )ret;
    }

    TrackJobCpu::TrackJobCpu(
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT particles_buffer,
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT beam_elements_buffer,
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT output_buffer,
        TrackJobCpu::size_type const until_turn,
        TrackJobCpu::size_type const num_elem_by_elem_turns  ) :
        TrackJobBase(),
        m_elem_by_elem_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_beam_monitor_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_particle_block_idx( TrackJobCpu::size_type{ 0 } ),
        m_owns_output_buffer( false )

    {
        using size_t  = TrackJobCpu::size_type;
        using index_t = NS(particle_index_t);

        index_t min_turn_id = index_t{ -1 };

        ::NS(Particles)* particles = ::NS(Particles_buffer_get_particles)(
            particles_buffer, this->m_particle_block_idx );

        SIXTRL_ASSERT( particles != nullptr );

        int ret = NS(OutputBuffer_prepare)( beam_elements_buffer,
            output_buffer, particles, num_elem_by_elem_turns,
            &this->m_elem_by_elem_index_offset,
            &this->m_beam_monitor_index_offset,
            &min_turn_id );

        SIXTRL_ASSERT( ret == 0 );

        ret = NS(BeamMonitor_assign_output_buffer_from_offset)(
            beam_elements_buffer, output_buffer, min_turn_id,
            this->m_beam_monitor_index_offset );

        SIXTRL_ASSERT( ret == 0 );

        if( num_elem_by_elem_turns > ( size_t{ 0 } ) )
        {
            ::NS(Particles)* elem_by_elem_particles =
                NS(Particles_buffer_get_particles)(
                    output_buffer, this->m_elem_by_elem_index_offset );

            SIXTRL_ASSERT( elem_by_elem_particles != nullptr );

            ret = NS(Track_all_particles_element_by_element_until_turn)(
                particles, beam_elements_buffer,
                min_turn_id + num_elem_by_elem_turns, elem_by_elem_particles );

            SIXTRL_ASSERT( ret == 0 );
        }

        if( until_turn > size_t{ 0 } )
        {
            ret = NS(Track_all_particles_until_turn)(
                particles, beam_elements_buffer, until_turn );

            SIXTRL_ASSERT( ret == 0 );
        }

        this->doSetPtrToParticlesBuffer( particles_buffer );
        this->doSetPtrToBeamElementsBuffer( beam_elements_buffer );
        this->doSetPtrToOutputBuffer( output_buffer );

        ( void )ret;<commit_msg>sixtracklib/common: Use buffer initialization routine from base in CPU track job<commit_after>#include "sixtracklib/common/track_job_cpu.h"

#include <algorithm>
#include <utility>

#if defined( __cplusplus )
    #include "sixtracklib/common/buffer.hpp"
#endif /* defined( __cplusplus ) */

#include "sixtracklib/common/definitions.h"
#include "sixtracklib/common/buffer.h"
#include "sixtracklib/common/particles.h"
#include "sixtracklib/common/internal/track_job_base.h"
#include "sixtracklib/common/output/output_buffer.h"
#include "sixtracklib/common/output/elem_by_elem_config.h"
#include "sixtracklib/common/track.h"


namespace SIXTRL_CXX_NAMESPACE
{
    TrackJobCpu::TrackJobCpu(
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT particles_buffer,
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT beam_elements_buffer,
        TrackJobCpu::size_type const until_turn,
        TrackJobCpu::size_type const num_elem_by_elem_turns  ) :
        TrackJobCpu::_base_t(),
        m_elem_by_elem_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_beam_monitor_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_particle_block_idx( TrackJobCpu::size_type{ 0 } ),
        m_owns_output_buffer( false )
    {
        using _this_t       = TrackJobCpu;
        using _base_t       = _this_t::_base_t;
        using c_buffer_t    = _this_t::c_buffer_t;
        using size_t        = _this_t::size_type;
        using index_t       = _this_t::particle_index_t;

        index_t min_turn_id = index_t{ -1 };

        c_buffer_t* output_buffer  = ::NS(Buffer_new)( size_t{ 0 } );
        this->m_owns_output_buffer = ( output_buffer != nullptr );

        bool success = _base_t::doInitBuffers( particles_buffer,
            beam_elements_buffer, output_buffer, num_elem_by_elem_turns,
            until_turn, &this->m_particle_block_idx, size_t{ 1 },
            &this->m_elem_by_elem_index_offset,
            &this->m_beam_monitor_index_offset, &min_turn_id );

        ::NS(Particles)* particles = ::NS(Particles_buffer_get_particles)(
            particles_buffer, this->m_particle_block_idx );

        success &= ( particles != nullptr );

        if( num_elem_by_elem_turns > ( size_t{ 0 } ) )
        {
            ::NS(Particles)* elem_by_elem_particles =
                NS(Particles_buffer_get_particles)(
                    output_buffer, this->m_elem_by_elem_index_offset );

            success &= ( elem_by_elem_particles != nullptr );

            success &= ( 0 ==
                ::NS(Track_all_particles_element_by_element_until_turn)(
                    particles, beam_elements_buffer,
                    min_turn_id + num_elem_by_elem_turns,
                    elem_by_elem_particles ) );
        }

        if( until_turn > size_t{ 0 } )
        {
            success &= ( 0 == ::NS(Track_all_particles_until_turn)(
                particles, beam_elements_buffer, until_turn ) );
        }

        SIXTRL_ASSERT( success );
        ( void )success;
    }

    TrackJobCpu::TrackJobCpu(
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT particles_buffer,
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT beam_elements_buffer,
        TrackJobCpu::c_buffer_t* SIXTRL_RESTRICT output_buffer,
        TrackJobCpu::size_type const until_turn,
        TrackJobCpu::size_type const num_elem_by_elem_turns  ) :
        TrackJobCpu::_base_t(),
        m_elem_by_elem_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_beam_monitor_index_offset( TrackJobCpu::size_type{ 0 } ),
        m_particle_block_idx( TrackJobCpu::size_type{ 0 } ),
        m_owns_output_buffer( false )
    {
        using _this_t       = TrackJobCpu;
        using _base_t       = _this_t::_base_t;
        using size_t        = _this_t::size_type;
        using index_t       = _this_t::particle_index_t;

        index_t min_turn_id = index_t{ -1 };

        bool success = _base_t::doInitBuffers( particles_buffer,
            beam_elements_buffer, output_buffer, num_elem_by_elem_turns,
            until_turn, &this->m_particle_block_idx, size_t{ 1 },
            &this->m_elem_by_elem_index_offset,
            &this->m_beam_monitor_index_offset, &min_turn_id );

        ::NS(Particles)* particles = ::NS(Particles_buffer_get_particles)(
            particles_buffer, this->m_particle_block_idx );

        if( num_elem_by_elem_turns > ( size_t{ 0 } ) )
        {
            ::NS(Particles)* elem_by_elem_particles =
                NS(Particles_buffer_get_particles)(
                    output_buffer, this->m_elem_by_elem_index_offset );

            success &= ( elem_by_elem_particles != nullptr );

            success &= ( 0 ==
                ::NS(Track_all_particles_element_by_element_until_turn)(
                    particles, beam_elements_buffer,
                    min_turn_id + num_elem_by_elem_turns,
                    elem_by_elem_particles ) );
        }

        if( until_turn > size_t{ 0 } )
        {
            success &= ( 0 == ::NS(Track_all_particles_until_turn)(
                particles, beam_elements_buffer, until_turn ) );
        }

        SIXTRL_ASSERT( success );
        ( void )success;
{'fraction_non_alphanumeric': 0.09212132594609823, 'fraction_numerical': 0.0037562212414311204, 'mean_word_length': 2.712094806552806, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4814585', 'n_tokens_mistral': 3609, 'n_tokens_neox': 3492, 'n_words': 498}