Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/soa'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.gitignore
#	src/main.cpp
#	src/parallel/simd/rt_sse.h
#	src/parallel/simd/scene.h
#	src/parallel/simd/utils/rt_helper.h
#	src/parallel/simd/utils/types.h
#	src/xmake.lua
#	tests/test_sse.cpp
  • Loading branch information
KVM-Explorer committed Jul 19, 2024
2 parents d34421e + bac2963 commit eec7234
Show file tree
Hide file tree
Showing 28 changed files with 1,036 additions and 885 deletions.
78 changes: 78 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "config.h"
Application::Application() {
app.description("Parallel Path Tracing in C++ with SIMD|SoA|OpenMP|CUDA");
app.name("PathTracer");

app.add_option("-o,--output", output, "Output file name");
app.add_option("--width", width, "Image width");
app.add_option("--height", height, "Image height");
app.add_option("-s,--samples", samples, "Samples per pixel");
app.add_option("-t,--threads", threads, "Number of threads");
app.add_option("-m,--mode", executeMode, "Mode: CPU, SoA, SIMD_SSE, SIMD_AVX, SIMD_AVX_512, RawThread, OpenMP, Async")->required();
app.add_option("-p,--profile", profile, "Profile mode");
app.add_option("-l,--load", loadType, "Thread load type: Row, Column, Task");
app.add_option("--task", taskSize, "Task size");
app.add_option("--scene", scene, "Scene name");
app.allow_extras();
}

int Application::run(int argc, char **argv) {
CLI11_PARSE(app, argc, argv);
return 0;
}

void Application::print() {
std::cout << "Configuration:" << std::endl;
std::cout << "Image: " << width << "x" << height << std::endl;
std::cout << "Samples: " << samples << std::endl;
std::cout << "ThreadNum: " << threads << std::endl;
std::cout << "TaskSize: " << taskSize << std::endl;
std::cout << "SplitThreadLoadType: " << loadType << std::endl;
std::cout << "OpenMPMode: " << openMPMode << std::endl;
}

ExecuteMode Application::getExecuteMode() {
if (executeMode == "CPU") {
return ExecuteMode::CPU;
} else if (executeMode == "SoA") {
return ExecuteMode::SoA;
} else if (executeMode == "SIMD_SSE") {
return ExecuteMode::SIMD_SSE;
} else if (executeMode == "SIMD_AVX") {
return ExecuteMode::SIMD_AVX;
} else if (executeMode == "SIMD_AVX_512") {
return ExecuteMode::SIMD_AVX_512;
} else if (executeMode == "RawThread") {
return ExecuteMode::RawThread;
} else if (executeMode == "OpenMP") {
return ExecuteMode::OpenMP;
} else if (executeMode == "Async") {
return ExecuteMode::Async;
} else {
return ExecuteMode::CPU;
}
}

ThreadLoadType Application::getThreadLoadType() {
if (loadType == "Row") {
return ThreadLoadType::Row;
} else if (loadType == "Column") {
return ThreadLoadType::Column;
} else if (loadType == "Block") {
return ThreadLoadType::Block;
} else {
return ThreadLoadType::Row;
}
}

OpenMPMode Application::getOpenMPMode() {
if (openMPMode == "ParallelFor") {
return OpenMPMode::ParallelFor;
} else if (openMPMode == "ParallelTask") {
return OpenMPMode::ParallelTask;
} else if (openMPMode == "ParallelWorkStealing") {
return OpenMPMode::ParallelWorkStealing;
} else {
return OpenMPMode::ParallelFor;
}
}
57 changes: 57 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>

enum class ExecuteMode {
CPU,
SoA,
SIMD_SSE,
SIMD_AVX,
SIMD_AVX_512,
RawThread,
OpenMP,
Async,
};

enum class ThreadLoadType {
Row,
Column,
Block,
};

enum class OpenMPMode {
ParallelFor,
ParallelTask,
ParallelWorkStealing,
};

class Application {
public:
Application();
~Application(){};
int run(int argc, char **argv);
ThreadLoadType getThreadLoadType();
ExecuteMode getExecuteMode();
OpenMPMode getOpenMPMode();
void print();
std::string name() const { return executeMode; }
std::string getLogLevel() const { return logLevel; }

public:
CLI::App app;
std::string output = "image.ppm";
int width = 512;
int height = 512;
int samples = 1;
int threads = 8;
bool profile = true;
int taskSize = 1 * 1;
std::string scene = "basic";

private:
std::string loadType = "Row";
std::string openMPMode = "ParallelFor";
std::string executeMode = "CPU";
std::string logLevel = "off";
};
38 changes: 38 additions & 0 deletions src/debug_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 实现一个输出到文件的静态类,方便全局调用
// 基于spdlog实现,写入到文件

#pragma once

#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>

class DebugHelper {
public:
static void init(std::string name = "default", std::string level = "error") {
auto log_level = getLevel(level);
auto path = std::format("logs/{}/{}.log", level, name);
auto logger = spdlog::basic_logger_mt("basic_logger", path);
spdlog::set_default_logger(logger);
spdlog::set_level(log_level);
spdlog::set_pattern("[%l] %v");
}

static spdlog::level::level_enum getLevel(std::string &level) {
if (level == "trace") {
return spdlog::level::trace;
} else if (level == "debug") {
return spdlog::level::debug;
} else if (level == "info") {
return spdlog::level::info;
} else if (level == "warn") {
return spdlog::level::warn;
} else if (level == "error") {
return spdlog::level::err;
} else if (level == "critical") {
return spdlog::level::critical;
} else {
level = "off";
return spdlog::level::off;
}
}
};
78 changes: 43 additions & 35 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "camera.h"
#include "debug_helper.h"
#include "image.h"
#include "parallel/simd/rt_sse.h"
#include "parallel/simd/rt_simd.h"
#include "parallel/simd/simd_helper.h"
#include "parallel/soa/rt_soa.h"
#include "parallel/thread/rt_openmp.h"
#include "parallel/thread/rt_thead.h"
#include "profile.h"
Expand All @@ -9,62 +12,66 @@
#include "scene.h"
#include <iostream>

enum class Mode {
CPU,
SIMD_SSE,
SIMD_AVX,
SIMD_AVX_512,
RawThread,
OpenMP,
Async,
};

int main(int argc, char *argv[]) {

// configuration
Application app;
app.run(argc, argv);

Image image("image.ppm", 512, 512);
Mode mode = Mode::CPU;
Camera cam = Camera(Vec(50, 52, 295.6), Vec(0, -0.042612, -1).norm(), image);
Scene scene = CornellBox();
int samples = 1; // 2 * 2 * samples per pixel
bool profile = true;
int threadNum = 8;
ThreadLoadType splitThreadLoadType = ThreadLoadType::Row;
OpenMPMode openmpMode = OpenMPMode::ParallelTask;
int taskSize = 1 * 1;

if (argc > 1) {
// Parse the taskSize argument
taskSize = std::atoi(argv[1]);
std::cout << std::format("Input taskSize: {}\n", taskSize);
DebugHelper::init(app.name(), app.getLogLevel());

// samples = std::atoi(argv[1]);
// std::cout << std::format("Input samples: {} sample rays: {}\n", samples, samples * 4);
Image image(app.output, app.width, app.height);
Camera cam = Camera(Vec(50, 52, 295.6), Vec(0, -0.042612, -1).norm(), image);
Scene scene;
if (app.scene == "basic") {
scene = CornellBox();
} else {
int n = 64;
std::from_chars(app.scene.data(), app.scene.data() + app.scene.size(), n);
scene = CornelBoxWithPlantySphere(n);
}
int samples = app.samples; // 2 * 2 * samples per pixel
bool profile = app.profile;
int threadNum = app.threads;
ThreadLoadType splitThreadLoadType = app.getThreadLoadType();
OpenMPMode openmpMode = app.getOpenMPMode();
int taskSize = app.taskSize;

// print configuration
app.print();

// render
std::unique_ptr<RayTracer> raytracer;
switch (mode) {
case Mode::CPU: {
switch (app.getExecuteMode()) {
case ExecuteMode::CPU: {
std::cout << "Mode: CPU" << std::endl;
raytracer = std::make_unique<RtCpu>(image, cam, scene, samples);
break;
}
case Mode::SIMD_SSE: {
raytracer = std::make_unique<RtOptimzationSSE>(image, cam, scene, samples);
case ExecuteMode::SoA: {
std::cout << "Mode: SoA" << std::endl;
raytracer = std::make_unique<RayTracingSoA>(image, cam, scene, samples);
break;
}
case ExecuteMode::SIMD_SSE: {
std::cout << "Mode: SIMD_SSE" << std::endl;
raytracer = std::make_unique<RayTracingSIMD>(image, cam, scene, samples);
break;
}
case Mode::RawThread: {
case ExecuteMode::RawThread: {
std::cout << "Mode: RawThread" << std::endl;
raytracer = std::make_unique<RtThread>(image, cam, scene, samples, threadNum, taskSize, splitThreadLoadType);
break;
}

case Mode::OpenMP: {
raytracer = std::make_unique<RtOpenMP>(image, cam, scene, samples,openmpMode,taskSize);
case ExecuteMode::OpenMP: {
std::cout << "Mode: OpenMP" << std::endl;
raytracer = std::make_unique<RtOpenMP>(image, cam, scene, samples, openmpMode, taskSize);
break;
}

default:
std::cout << "Unknown Mode, Select Default Mode CPU" << std::endl;
raytracer = std::make_unique<RtCpu>(image, cam, scene, samples);
}

Expand All @@ -81,4 +88,5 @@ int main(int argc, char *argv[]) {
std::cout << "Image saved to image.ppm" << std::endl;
// output
image.save();
spdlog::shutdown();
}
Loading

0 comments on commit eec7234

Please sign in to comment.