資料分析 GPU 應用:權證行情

程式碼:  (Linux 版)

#include <arrayfire.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <exception>

#define SHOW_MSG	0

using namespace af;
using namespace std;

class SecondCup {
private:
	vector<std::string> stockNames;
	vector<af::array> vDataGPU;
	vector<af::array> aDataGPU;
	vector<float> result[4];
	
	vector<string> split(string str, char delimiter);
public:
	SecondCup(int idGPU);
	~SecondCup(void);
	
	void loadDataFromFile(const std::string dataFile);
	void calcVelocity(void);
	void calcAcceration(void);

	size_t getRowCount();
	std::string getStockID(int id);
	vector<float> getResult(int id);
};

SecondCup::SecondCup(int idGPU) {
	af::setDevice(idGPU);
#if SHOW_MSG==1
	af::info();
#endif
}

SecondCup::~SecondCup(void) {}

vector<string> SecondCup::split(string str, char delimiter) {
	vector<string> internal;
	stringstream ss(str);
	string tok;

	while (getline(ss, tok, delimiter)) {
		if (tok.empty()) continue;

		internal.push_back(tok);
	}

	return internal;
}

void SecondCup::loadDataFromFile(const std::string dataFile) {
	std::ifstream fileData;
	std::string lineData;

	fileData.open(dataFile.c_str());

	while (std::getline(fileData, lineData))
	{
		vector<string> fields = this->split(lineData, ',');

		this->stockNames.push_back(fields[0]);

#if SHOW_MSG==1
		std::cout << fields[0] << '\t';
#endif
		vector<float> rawData;

		for (int i = 1; i < fields.size(); i++) {
			float v;

			try {
				v = std::stof(fields[i]);
				rawData.push_back(v);
			}
			catch (std::exception &e) {
#if SHOW_MSG==1
				std::cout << '(' << i << ',' << fields[i] << ')';
#endif
			}
		}

		af::array rwa = af::array(1, rawData.size(), rawData.data(), afHost);

#if SHOW_MSG==1
		af_print(af::diff1(rwa, 1));
#endif

		// 計算速率
		this->vDataGPU.push_back(af::diff1(rwa, 1));

		// 計算加速度
		this->aDataGPU.push_back(af::diff2(rwa, 1));

		rwa.lock();
	}

	std::cout << std::endl;
}

void SecondCup::calcVelocity(void) {
	this->result[0].empty();
	this->result[1].empty();

	for (int i=0; i<this->vDataGPU.size(); i++)	{
		this->result[0].push_back(af::mean(this->vDataGPU[i]).host<float>()[0]);
		this->result[1].push_back(af::stdev(this->vDataGPU[i]).host<float>()[0]);
	}
}

void SecondCup::calcAcceration(void) {
	this->result[2].empty();
	this->result[3].empty();

	for (int i = 0; i < this->aDataGPU.size(); i++) {
		this->result[2].push_back(af::mean(this->aDataGPU[i]).host<float>()[0]);
		this->result[3].push_back(af::stdev(this->aDataGPU[i]).host<float>()[0]);
	}
}

size_t SecondCup::getRowCount() {
	return this->stockNames.size();
}

std::string SecondCup::getStockID(int id) {
	return this->stockNames.at(id);
}

vector<float>  SecondCup::getResult(int id) {
	return this->result[id];
}

int main(int argc, char *argv[])
{
	std::string dataFile = "StockMarketHistory.csv";
	SecondCup *worker = new SecondCup(0);

    try {
		worker->loadDataFromFile(dataFile);

		worker->calcVelocity();
		worker->calcAcceration();

		for (unsigned i = 0; i < worker->getRowCount(); i++) {
			std::cout << worker->getStockID(i) << '\t';
			std::cout << std::fixed << std::setw(9) << std::setprecision(3) << worker->getResult(0).at(i) << '\t';
			std::cout << std::fixed << std::setw(9) << std::setprecision(3) << worker->getResult(1).at(i) << '\t';
			std::cout << std::fixed << std::setw(9) << std::setprecision(3) << worker->getResult(2).at(i) << '\t';
			std::cout << std::fixed << std::setw(9) << std::setprecision(3) << worker->getResult(3).at(i) << endl;
		}
	} catch (af::exception& e) {
        std::cerr << e.what() << endl;
        throw;
    }

    return 0;
}

輸出

ċ
StockMarketHistory.csv
(4380k)
李智,
2016年7月14日 上午3:49