Skip to content

Commit

Permalink
Add serial model
Browse files Browse the repository at this point in the history
  • Loading branch information
tom91136 authored and gonzalobg committed Jun 5, 2024
1 parent 616b3b2 commit 2929a6c
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ endif ()
include(cmake/register_models.cmake)

# register out models <model_name> <preprocessor_def_name> <source files...>
register_model(serial SERIAL SerialStream.cpp)
register_model(omp OMP OMPStream.cpp)
register_model(ocl OCL OCLStream.cpp)
register_model(std-data STD_DATA STDDataStream.cpp)
Expand Down
6 changes: 6 additions & 0 deletions src/StreamModels.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "SYCLStream2020.h"
#elif defined(OMP)
#include "OMPStream.h"
#elif defined(SERIAL)
#include "SerialStream.h"
#elif defined(FUTHARK)
#include "FutharkStream.h"
#endif
Expand Down Expand Up @@ -93,6 +95,10 @@ std::unique_ptr<Stream<T>> make_stream(Args... args) {
// Use the OpenMP implementation
return std::make_unique<OMPStream<T>>(args...);

#elif defined(SERIAL)
// Use the Serial implementation
return std::make_unique<SerialStream<T>>(array_size, deviceIndex);

#elif defined(FUTHARK)
// Use the Futhark implementation
return std::make_unique<FutharkStream<T>>(args...);
Expand Down
4 changes: 4 additions & 0 deletions src/ci-test-compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ build_gcc() {
local name="gcc_build"
local cxx="-DCMAKE_CXX_COMPILER=${GCC_CXX:?}"

run_build $name "${GCC_CXX:?}" serial "$cxx"
run_build $name "${GCC_CXX:?}" omp "$cxx"
if [ "$MODEL" = "all" ] || [ "$MODEL" = "OMP" ]; then
# sanity check that it at least runs
Expand Down Expand Up @@ -219,6 +220,7 @@ build_gcc() {
build_clang() {
local name="clang_build"
local cxx="-DCMAKE_CXX_COMPILER=${CLANG_CXX:?}"
run_build $name "${CLANG_CXX:?}" serial "$cxx"
run_build $name "${CLANG_CXX:?}" omp "$cxx"

if [ "${CLANG_OMP_OFFLOAD_AMD:-false}" != "false" ]; then
Expand Down Expand Up @@ -276,6 +278,7 @@ build_nvhpc() {
}

build_aocc() {
run_build aocc_build "${AOCC_CXX:?}" serial "-DCMAKE_CXX_COMPILER=${AOCC_CXX:?}"
run_build aocc_build "${AOCC_CXX:?}" omp "-DCMAKE_CXX_COMPILER=${AOCC_CXX:?}"
}

Expand Down Expand Up @@ -309,6 +312,7 @@ build_icpc() {
set -u
local name="intel_build"
local cxx="-DCMAKE_CXX_COMPILER=${ICPC_CXX:?}"
run_build $name "${ICPC_CXX:?}" serial "$cxx"
run_build $name "${ICPC_CXX:?}" omp "$cxx"
run_build $name "${ICPC_CXX:?}" ocl "$cxx -DOpenCL_LIBRARY=${OCL_LIB:?}"
if check_cmake_ver "3.20.0"; then
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define VERSION_STRING "5.0"

#include "Stream.h"

#include "StreamModels.h"
#include "Unit.h"

Expand Down
134 changes: 134 additions & 0 deletions src/serial/SerialStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@

// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith, Tom Lin
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code

#include <cstdlib> // For aligned_alloc
#include "SerialStream.h"

#ifndef ALIGNMENT
#define ALIGNMENT (2*1024*1024) // 2MB
#endif

template <class T>
SerialStream<T>::SerialStream(const intptr_t ARRAY_SIZE, int device)
{
array_size = ARRAY_SIZE;

// Allocate on the host
this->a = (T*)aligned_alloc(ALIGNMENT, sizeof(T)*array_size);
this->b = (T*)aligned_alloc(ALIGNMENT, sizeof(T)*array_size);
this->c = (T*)aligned_alloc(ALIGNMENT, sizeof(T)*array_size);
}

template <class T>
SerialStream<T>::~SerialStream()
{
free(a);
free(b);
free(c);
}

template <class T>
void SerialStream<T>::init_arrays(T initA, T initB, T initC)
{
intptr_t array_size = this->array_size;
for (intptr_t i = 0; i < array_size; i++)
{
a[i] = initA;
b[i] = initB;
c[i] = initC;
}
}

template <class T>
void SerialStream<T>::read_arrays(std::vector<T>& h_a, std::vector<T>& h_b, std::vector<T>& h_c)
{
for (intptr_t i = 0; i < array_size; i++)
{
h_a[i] = a[i];
h_b[i] = b[i];
h_c[i] = c[i];
}

}

template <class T>
void SerialStream<T>::copy()
{
for (intptr_t i = 0; i < array_size; i++)
{
c[i] = a[i];
}
}

template <class T>
void SerialStream<T>::mul()
{
const T scalar = startScalar;
for (intptr_t i = 0; i < array_size; i++)
{
b[i] = scalar * c[i];
}
}

template <class T>
void SerialStream<T>::add()
{
for (intptr_t i = 0; i < array_size; i++)
{
c[i] = a[i] + b[i];
}
}

template <class T>
void SerialStream<T>::triad()
{
const T scalar = startScalar;
for (intptr_t i = 0; i < array_size; i++)
{
a[i] = b[i] + scalar * c[i];
}
}

template <class T>
void SerialStream<T>::nstream()
{
const T scalar = startScalar;
for (intptr_t i = 0; i < array_size; i++)
{
a[i] += b[i] + scalar * c[i];
}
}

template <class T>
T SerialStream<T>::dot()
{
T sum{};
for (intptr_t i = 0; i < array_size; i++)
{
sum += a[i] * b[i];
}
return sum;
}



void listDevices(void)
{
std::cout << "0: CPU" << std::endl;
}

std::string getDeviceName(const int)
{
return std::string("Device name unavailable");
}

std::string getDeviceDriver(const int)
{
return std::string("Device driver unavailable");
}
template class SerialStream<float>;
template class SerialStream<double>;
43 changes: 43 additions & 0 deletions src/serial/SerialStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith, Tom Lin
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code

#pragma once

#include <iostream>
#include <stdexcept>

#include "Stream.h"


#define IMPLEMENTATION_STRING "Serial"

template <class T>
class SerialStream : public Stream<T>
{
protected:
// Size of arrays
intptr_t array_size;

// Device side pointers
T *a;
T *b;
T *c;

public:
SerialStream(const intptr_t, int);
~SerialStream();

virtual void copy() override;
virtual void add() override;
virtual void mul() override;
virtual void triad() override;
virtual void nstream() override;
virtual T dot() override;

virtual void init_arrays(T initA, T initB, T initC) override;
virtual void read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c) override;
};
4 changes: 4 additions & 0 deletions src/serial/model.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
macro(setup)
# Nothing to do
endmacro()

0 comments on commit 2929a6c

Please sign in to comment.