<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "consumer.hh"
#include "globals.hh"

using namespace boost::filesystem;
using namespace std;

#include <fstream>
Consumer::Consumer(string throwaway)
    : _playlist_file_(std::move(throwaway))
    , _write_to_file_(true) {

    std::ifstream file(_playlist_file_);

    if (file) {
        string temp;
        while (getline(file, temp)) {
            _playlist_lines_.insert(temp);
        }
    }
}

void Consumer::_sorted_insert_song_(const std::string& str) {
    bool inserted = _playlist_lines_.insert(str).second;
    if (NONO && inserted) {
        printf("Insert `%s` into playlist `%s`.\n", str.c_str(),
               _playlist_file_.c_str());
    }
}

Consumer::~Consumer() {
    if (_write_to_file_) {
        if (!NONO) {
            std::ofstream file(_playlist_file_);
            for (const auto& s : _playlist_lines_) {
                file << s << '\n';
            }
            if (USE_MPC && system("mpc -q") == 0) {
                system("mpc -q clear");
                system("mpc -q update");
                string pl =
                    "mpc -q load " +
                    path(_playlist_file_).filename().replace_extension().string();
                system(pl.c_str());
            }
        } else if (USE_MPC) {
            printf("$ mpc clear\n");
            string pl = path(_playlist_file_).filename().replace_extension().string();
            printf("$ mpc load %s\n", pl.c_str());
        }
    }
}

#include <taglib/fileref.h>
void Consumer::_apply_tags_(const path& path, const string& artist,
                            const string& title) {
    using namespace TagLib;
    FileRef file(path.c_str());
    Tag* tags = file.tag();

    if (!tags) {
        fprintf(stderr, "Music file `%s` has no tags!\n", path.c_str());
        return;
    }

    if (tags->artist() != artist) {
        if (NONO) {
            printf("  Artist: `%s` -> `%s`\n",
                   tags->artist().toCString(), artist.c_str());
        } else {
            tags->setArtist(artist);
        }
    }

    if (tags->title() != title) {
        if (NONO) {
            printf("  Title: `%s` -> `%s`\n",
                   tags->title().toCString(), title.c_str());
        } else {
            tags->setTitle(title);
        }
    }

    if (!NONO) {
        file.save();
    }
}
<commit_msg>Echo `$ mpc update` in NONO mode<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "consumer.hh"
#include "globals.hh"

using namespace boost::filesystem;
using namespace std;

#include <fstream>
Consumer::Consumer(string throwaway)
    : _playlist_file_(std::move(throwaway))
    , _write_to_file_(true) {

    std::ifstream file(_playlist_file_);

    if (file) {
        string temp;
        while (getline(file, temp)) {
            _playlist_lines_.insert(temp);
        }
    }
}

void Consumer::_sorted_insert_song_(const std::string& str) {
    bool inserted = _playlist_lines_.insert(str).second;
    if (NONO && inserted) {
        printf("Insert `%s` into playlist `%s`.\n", str.c_str(),
               _playlist_file_.c_str());
    }
}

Consumer::~Consumer() {
    if (_write_to_file_) {
        if (!NONO) {
            std::ofstream file(_playlist_file_);
            for (const auto& s : _playlist_lines_) {
                file << s << '\n';
            }
            if (USE_MPC && system("mpc -q") == 0) {
                system("mpc -q clear");
                system("mpc -q update");
                string pl =
                    "mpc -q load " +
                    path(_playlist_file_).filename().replace_extension().string();
                system(pl.c_str());
            }
        } else if (USE_MPC) {
            printf("$ mpc clear\n");
            printf("$ mpc update\n");
            string pl = path(_playlist_file_).filename().replace_extension().string();
            printf("$ mpc load %s\n", pl.c_str());
        }
    }
}

#include <taglib/fileref.h>
void Consumer::_apply_tags_(const path& path, const string& artist,
                            const string& title) {
    using namespace TagLib;
    FileRef file(path.c_str());
    Tag* tags = file.tag();

    if (!tags) {
        fprintf(stderr, "Music file `%s` has no tags!\n", path.c_str());
        return;
    }

    if (tags->artist() != artist) {
        if (NONO) {
            printf("  Artist: `%s` -> `%s`\n",
                   tags->artist().toCString(), artist.c_str());
        } else {
            tags->setArtist(artist);
        }
    }

    if (tags->title() != title) {
        if (NONO) {
            printf("  Title: `%s` -> `%s`\n",
                   tags->title().toCString(), title.c_str());
        } else {
            tags->setTitle(title);
        }
    }

    if (!NONO) {
        file.save();
    }
}
<|endoftext|>


<commit_before>#include "CpuID.hpp"

namespace cos
{
	CpuID::Vendor CpuID::getVendor()
	{
		if(vendor == Vendor::NotSet)
			findVendor();

		return vendor;
	}

	const char* CpuID::getVendorString()
	{
		if(vendor == Vendor::NotSet)
			findVendor();

		return VENDOR_STRINGS[static_cast<uint8>(vendor)];
	}

	const char* CpuID::getModelString()
	{
		if(modelString[0] == 0)
			calcBrandString();

		return modelString;
	}

	uint8 CpuID::getStepID()
	{
		if(displayStepID == 0)
			getVersion();

		return displayStepID;
	}

	uint8 CpuID::getModelID()
	{
		if(displayModelID == 0)
			getVersion();

		return displayModelID;
	}

	uint8 CpuID::getFamilyID()
	{
		if(displayFamilyID == 0)
			getVersion();

		return displayFamilyID;
	}

	uint8 CpuID::getTypeID()
	{
		if(displayTypeID == 0)
			getVersion();

		return displayTypeID;
	}

	uint16 CpuID::getClflushLineSize()
	{
		if(clflushLineSize == 0)
			getVersion();

		return clflushLineSize;
	}

	uint8 CpuID::getNumLogicalProcessors()
	{
		if(numLogicalProcessors == 0)
			getVersion();

		return numLogicalProcessors;
	}

	uint8 CpuID::getInitialAPICID()
	{
		if(initialAPICID == 0)
			getVersion();

		return initialAPICID;
	}

	bool CpuID::hasFeature(FeatureECX f)
	{
		if(featuresECX == 0)
			getVersion();

		return featuresECX & static_cast<uint32>(f);
	}

	bool CpuID::hasFeature(FeatureEDX f)
	{
		if(featuresEDX == 0)
			getVersion();

		return featuresEDX & static_cast<uint32>(f);
	}

	bool CpuID::cpuid(uint32 in, uint32 out[4])
	{
		if(in > maxValueEAX && (in < 0x80000000 || in > maxExtValueEAX))	// invalid value for eax
		{
			return false;
		}

		asm volatile("cpuid": "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3]) : "a"(in));

		return true;
	}

	void CpuID::findVendor()
	{
		uint32 registers[4] = {0, 0, 0, 0};

		cpuid(0x0, registers);

		// update maxValue for eax
		maxValueEAX = registers[0];

		// Intel check
		if(	registers[1] == VENDOR_CODES[0][0] &&
			registers[2] == VENDOR_CODES[0][1] &&
			registers[3] == VENDOR_CODES[0][2])
		{
			vendor = Vendor::Intel;
		}
		// AMD check
		else if(registers[1] == VENDOR_CODES[1][0] &&
		        registers[2] == VENDOR_CODES[1][1] &&
		        registers[3] == VENDOR_CODES[1][2])
		{
			vendor = Vendor::AMD;
		}
		else
		{
			vendor = Vendor::Unknown;
		}

		getMaxExtValueEax();
	}

	void CpuID::getVersion()
	{
		if(vendor == Vendor::NotSet)
			findVendor();

		uint32 registers[4] = {0, 0, 0, 0};

		cpuid(0x1, registers);

		// parse eax
		displayStepID = registers[0] & 0xf;				// bits 0-3
		uint8 modelID = registers[0] >> 4 & 0xf;		// bits 4-7
		uint8 familyID = registers[0] >> 8 & 0xf;		// bits 8-11
		displayTypeID = registers[0] >> 12 & 0x3;		// bits 12-13
		uint8 extModelID = registers[0] >> 16 & 0xf;	// bits 16-19 *only needed when familyID is 0x6 or 0xf
		uint8 extFamilyID = registers[0] >> 20 & 0xff;	// bits 20-27 *only needed when familyID is 0xf

		// handle final familyID
		if(familyID == 0xf)
			displayFamilyID = familyID + extFamilyID;
		else
			displayFamilyID = familyID;

		// handle final modelID
		if(familyID == 0x6 || familyID == 0xf)
		 	displayModelID = extModelID << 4 | modelID;
		else
			displayModelID = modelID;

		// parse ebx
		//uint8 brandIdx = registers[1] & 0xff;	// brand string index is no longer used
		clflushLineSize = (registers[1] >> 8 & 0xff) * 8;
		numLogicalProcessors = registers[1] >> 16 & 0xff;
		initialAPICID = registers[1] >> 24 & 0xff;

		// parse ecx
		featuresECX = registers[2];

		// parse edx
		featuresEDX = registers[3];
	}

	void CpuID::getMaxExtValueEax()
	{
		uint32 registers[4] = {0, 0, 0, 0};

		cpuid(0x80000000, registers);

		maxExtValueEAX = registers[0];
	}

	// assuming brand string is supported
	void CpuID::calcBrandString()
	{
		if(maxExtValueEAX == 0)
			getMaxExtValueEax();

		uint32 registers[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		uint32* registersSet0 = &registers[0];	// bytes 0 - 15
		uint32* registersSet1 = &registers[4];	// bytes 16 - 31
		uint32* registersSet2 = &registers[8];	// bytes 32 - 47

		cpuid(0x80000002, registersSet0);
		cpuid(0x80000003, registersSet1);
		cpuid(0x80000004, registersSet2);

		for(int i = 0; i < 48; i += 4)
		{
			for(int j = 0; j < 4; j++)
			{
				modelString[i + j] = registers[i / 4] >> 8 * j & 0xff;
			}
		}

		modelString[46] = 'H';
	}

	const char* CpuID::VENDOR_STRINGS[3] =
	{
		"GenuineIntel",
		"AuthenticAMD",
		"Unknown",
	};

	const uint32 CpuID::VENDOR_CODES[3][3] =
	{
		// Intel
		{0x756e6547, 0x6c65746e, 0x49656e69},	// "uneG", "letn", "Ieni"
		// AMD
		{0x68747541, 0x444d4163, 0x69746e65},	// "htuA", "DMAc", "itne"
		// Unknown
		{0x0, 0x0, 0x0},
	};

	CpuID::Vendor CpuID::vendor = CpuID::Vendor::NotSet;

	uint8 CpuID::displayStepID = 0;
	uint8 CpuID::displayModelID = 0;
	uint8 CpuID::displayFamilyID = 0;
	uint8 CpuID::displayTypeID = 0;

	char CpuID::modelString[48] = {0};
	uint16 CpuID::clflushLineSize = 0;
	uint8 CpuID::numLogicalProcessors = 0;
	uint8 CpuID::initialAPICID = 0;

	uint32 CpuID::featuresECX = 0;
	uint32 CpuID::featuresEDX = 0;

	uint16 CpuID::maxValueEAX = 0;
	uint32 CpuID::maxExtValueEAX = 0x80000000;

	uint32 CpuID::maxValueECX = 0;
}
<commit_msg>Spacing fix.<commit_after>#include "CpuID.hpp"

namespace cos
{
	CpuID::Vendor CpuID::getVendor()
	{
		if(vendor == Vendor::NotSet)
			findVendor();

		return vendor;
	}

	const char* CpuID::getVendorString()
	{
		if(vendor == Vendor::NotSet)
			findVendor();

		return VENDOR_STRINGS[static_cast<uint8>(vendor)];
	}

	const char* CpuID::getModelString()
	{
		if(modelString[0] == 0)
			calcBrandString();

		return modelString;
	}

	uint8 CpuID::getStepID()
	{
		if(displayStepID == 0)
			getVersion();

		return displayStepID;
	}

	uint8 CpuID::getModelID()
	{
		if(displayModelID == 0)
			getVersion();

		return displayModelID;
	}

	uint8 CpuID::getFamilyID()
	{
		if(displayFamilyID == 0)
			getVersion();

		return displayFamilyID;
	}

	uint8 CpuID::getTypeID()
	{
		if(displayTypeID == 0)
			getVersion();

		return displayTypeID;
	}

	uint16 CpuID::getClflushLineSize()
	{
		if(clflushLineSize == 0)
			getVersion();

		return clflushLineSize;
	}

	uint8 CpuID::getNumLogicalProcessors()
	{
		if(numLogicalProcessors == 0)
			getVersion();

		return numLogicalProcessors;
	}

	uint8 CpuID::getInitialAPICID()
	{
		if(initialAPICID == 0)
			getVersion();

		return initialAPICID;
	}

	bool CpuID::hasFeature(FeatureECX f)
	{
		if(featuresECX == 0)
			getVersion();

		return featuresECX & static_cast<uint32>(f);
	}

	bool CpuID::hasFeature(FeatureEDX f)
	{
		if(featuresEDX == 0)
			getVersion();

		return featuresEDX & static_cast<uint32>(f);
	}

	bool CpuID::cpuid(uint32 in, uint32 out[4])
	{
		if(in > maxValueEAX && (in < 0x80000000 || in > maxExtValueEAX))	// invalid value for eax
		{
			return false;
		}

		asm volatile("cpuid" : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3]) : "a"(in));

		return true;
	}

	void CpuID::findVendor()
	{
		uint32 registers[4] = {0, 0, 0, 0};

		cpuid(0x0, registers);

		// update maxValue for eax
		maxValueEAX = registers[0];

		// Intel check
		if(	registers[1] == VENDOR_CODES[0][0] &&
			registers[2] == VENDOR_CODES[0][1] &&
			registers[3] == VENDOR_CODES[0][2])
		{
			vendor = Vendor::Intel;
		}
		// AMD check
		else if(registers[1] == VENDOR_CODES[1][0] &&
		        registers[2] == VENDOR_CODES[1][1] &&
		        registers[3] == VENDOR_CODES[1][2])
		{
			vendor = Vendor::AMD;
		}
		else
		{
			vendor = Vendor::Unknown;
		}

		getMaxExtValueEax();
	}

	void CpuID::getVersion()
	{
		if(vendor == Vendor::NotSet)
			findVendor();

		uint32 registers[4] = {0, 0, 0, 0};

		cpuid(0x1, registers);

		// parse eax
		displayStepID = registers[0] & 0xf;				// bits 0-3
		uint8 modelID = registers[0] >> 4 & 0xf;		// bits 4-7
		uint8 familyID = registers[0] >> 8 & 0xf;		// bits 8-11
		displayTypeID = registers[0] >> 12 & 0x3;		// bits 12-13
		uint8 extModelID = registers[0] >> 16 & 0xf;	// bits 16-19 *only needed when familyID is 0x6 or 0xf
		uint8 extFamilyID = registers[0] >> 20 & 0xff;	// bits 20-27 *only needed when familyID is 0xf

		// handle final familyID
		if(familyID == 0xf)
			displayFamilyID = familyID + extFamilyID;
		else
			displayFamilyID = familyID;

		// handle final modelID
		if(familyID == 0x6 || familyID == 0xf)
		 	displayModelID = extModelID << 4 | modelID;
		else
			displayModelID = modelID;

		// parse ebx
		//uint8 brandIdx = registers[1] & 0xff;	// brand string index is no longer used
		clflushLineSize = (registers[1] >> 8 & 0xff) * 8;
		numLogicalProcessors = registers[1] >> 16 & 0xff;
		initialAPICID = registers[1] >> 24 & 0xff;

		// parse ecx
		featuresECX = registers[2];

		// parse edx
		featuresEDX = registers[3];
	}

	void CpuID::getMaxExtValueEax()
	{
		uint32 registers[4] = {0, 0, 0, 0};

		cpuid(0x80000000, registers);

		maxExtValueEAX = registers[0];
	}

	// assuming brand string is supported
	void CpuID::calcBrandString()
	{
		if(maxExtValueEAX == 0)
			getMaxExtValueEax();

		uint32 registers[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		uint32* registersSet0 = &registers[0];	// bytes 0 - 15
		uint32* registersSet1 = &registers[4];	// bytes 16 - 31
		uint32* registersSet2 = &registers[8];	// bytes 32 - 47

		cpuid(0x80000002, registersSet0);
		cpuid(0x80000003, registersSet1);
		cpuid(0x80000004, registersSet2);

		for(int i = 0; i < 48; i += 4)
		{
			for(int j = 0; j < 4; j++)
			{
				modelString[i + j] = registers[i / 4] >> 8 * j & 0xff;
			}
		}

		modelString[46] = 'H';
	}

	const char* CpuID::VENDOR_STRINGS[3] =
	{
		"GenuineIntel",
		"AuthenticAMD",
		"Unknown",
	};

	const uint32 CpuID::VENDOR_CODES[3][3] =
	{
		// Intel
		{0x756e6547, 0x6c65746e, 0x49656e69},	// "uneG", "letn", "Ieni"
		// AMD
		{0x68747541, 0x444d4163, 0x69746e65},	// "htuA", "DMAc", "itne"
		// Unknown
		{0x0, 0x0, 0x0},
	};

	CpuID::Vendor CpuID::vendor = CpuID::Vendor::NotSet;

	uint8 CpuID::displayStepID = 0;
	uint8 CpuID::displayModelID = 0;
	uint8 CpuID::displayFamilyID = 0;
	uint8 CpuID::displayTypeID = 0;

	char CpuID::modelString[48] = {0};
	uint16 CpuID::clflushLineSize = 0;
	uint8 CpuID::numLogicalProcessors = 0;
	uint8 CpuID::initialAPICID = 0;

	uint32 CpuID::featuresECX = 0;
	uint32 CpuID::featuresEDX = 0;

	uint16 CpuID::maxValueEAX = 0;
	uint32 CpuID::maxExtValueEAX = 0x80000000;

	uint32 CpuID::maxValueECX = 0;
}
<|endoftext|>


<commit_before>#include <mart-common/MartTime.h>
#include <mart-common/algorithm.h>
#include <mart-netlib/udp.hpp>

#include <atomic>
#include <fstream>
#include <iostream>
#include <optional>
#include <string_view>
#include <vector>

#include <im_str/im_str.hpp>

namespace udp = mart::nw::ip::udp;
using namespace std::chrono_literals;

std::vector<std::string_view> make_arglist( int argc, char** argv )
{
	std::vector<std::string_view> ret;
	for( int i = 0; i < argc; ++i ) {
		ret.emplace_back( argv[i] );
	}
	return ret;
}

std::optional<udp::endpoint> get_local_address( const std::vector<std::string_view>& args )
{
	return udp::try_parse_v4_endpoint( "127.0.0.1:3435" );
}

std::optional<std::string> get_filename( const std::vector<std::string_view>& args )
{
	return std::string{"Testfile"};
}

std::string_view to_stringview( mart::ConstMemoryView data )
{
	return std::string_view{data.asConstCharPtr(), data.size()};
}

int main( int argc, char** argv )
{
	const auto arglist = make_arglist( argc, argv );

	const udp::endpoint local_ep  = get_local_address( arglist ).value();
	const auto          file_name = get_filename( arglist ).value();

	udp::Socket sock;
	sock.set_rx_timeout( 1000ms );
	sock.bind( local_ep );

	std::cout << "Listening on " << local_ep.toStringEx() << " and writing to" << file_name << std::endl;

	std::string buffer( 1000, '\0' );

	while( true ) {
		auto res = sock.try_recv( mart::view_elements_mutable( buffer ).asBytes() );

		if( res.isValid() ) {
			auto msg = to_stringview( res );
			if( msg == "EXIT" ) { return 0; }
			std::ofstream file( file_name, std::ios_base::out | std::ios_base::trunc );
			file.seekp( 0 );
			file << msg << "\n";
			std::cout << "\"" << msg << "\"" << std::endl;
		}
	}
	std::cout << "Server shutting down\n" << std::flush;
}<commit_msg>[examples] fix warnings about unused parameters<commit_after>#include <mart-common/MartTime.h>
#include <mart-common/algorithm.h>
#include <mart-netlib/udp.hpp>

#include <atomic>
#include <fstream>
#include <iostream>
#include <optional>
#include <string_view>
#include <vector>

#include <im_str/im_str.hpp>

namespace udp = mart::nw::ip::udp;
using namespace std::chrono_literals;

std::vector<std::string_view> make_arglist( int argc, char** argv )
{
	std::vector<std::string_view> ret;
	for( int i = 0; i < argc; ++i ) {
		ret.emplace_back( argv[i] );
	}
	return ret;
}

std::optional<udp::endpoint> get_local_address( const std::vector<std::string_view>& )
{
	return udp::try_parse_v4_endpoint( "127.0.0.1:3435" );
}

std::optional<std::string> get_filename( const std::vector<std::string_view>& )
{
	return std::string{"Testfile"};
}

std::string_view to_stringview( mart::ConstMemoryView data )
{
	return std::string_view{data.asConstCharPtr(), data.size()};
}

int main( int argc, char** argv )
{
	const auto arglist = make_arglist( argc, argv );

	const udp::endpoint local_ep  = get_local_address( arglist ).value();
	const auto          file_name = get_filename( arglist ).value();

	udp::Socket sock;
	sock.set_rx_timeout( 1000ms );
	sock.bind( local_ep );

	std::cout << "Listening on " << local_ep.toStringEx() << " and writing to" << file_name << std::endl;

	std::string buffer( 1000, '\0' );

	while( true ) {
		auto res = sock.try_recv( mart::view_elements_mutable( buffer ).asBytes() );

		if( res.isValid() ) {
			auto msg = to_stringview( res );
			if( msg == "EXIT" ) { return 0; }
			std::ofstream file( file_name, std::ios_base::out | std::ios_base::trunc );
			file.seekp( 0 );
			file << msg << "\n";
			std::cout << "\"" << msg << "\"" << std::endl;
		}
	}
	std::cout << "Server shutting down\n" << std::flush;
}<|endoftext|>


<commit_before>/**
 * Copyright (c) 2016-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#include "Matrix.h"
#include "Vector.h"
#include "Dictionary.h"
#include "Model.h"
#include "Utils.h"
#include "Real.h"
#include "Args.h"
#include <iostream>
#include <iomanip>
#include <thread>
#include <time.h>
#include <string>
#include <math.h>
#include <vector>
#include <atomic>
#include <fenv.h>

Args args;

namespace info {
  clock_t start;
  std::atomic<int64_t> allWords(0);
  std::atomic<int64_t> allN(0);
  double allLoss(0.0);
}

void saveVectors(Dictionary& dict, Matrix& input, Matrix& output) {
  int32_t N = dict.getNumWords();
  std::wofstream ofs(args.output + ".vec");
  if (ofs.is_open()) {
    ofs << N << ' ' << args.dim << std::endl;
    for (int32_t i = 0; i < N; i++) {
      ofs << dict.getWord(i) << ' ';
      Vector embedding(args.dim);
      embedding.zero();
      const std::vector<int32_t>& ngrams = dict.getNgrams(i);
      for (auto it = ngrams.begin(); it != ngrams.end(); ++it) {
        embedding.addRow(input, *it);
      }
      embedding.mul(1.0 / ngrams.size());
      embedding.writeToStream(ofs);
      ofs << std::endl;
    }
    ofs.close();
  } else {
    std::wcout << "Error opening file for writing" << std::endl;
  }
}

void saveModel(Dictionary& dict, Matrix& input, Matrix& output) {
  std::ofstream ofs(args.output + ".bin");
  args.save(ofs);
  dict.save(ofs);
  input.save(ofs);
  output.save(ofs);
  ofs.close();
}

void loadModel(Dictionary& dict, Matrix& input, Matrix& output) {
  std::ifstream ifs(args.output + ".bin");
  args.load(ifs);
  dict.load(ifs);
  input.load(ifs);
  output.load(ifs);
  ifs.close();
}

void printInfo(Model& model, long long numTokens) {
  real progress = real(info::allWords) / (args.epoch * numTokens);
  real avLoss = info::allLoss / info::allN;
  float time = float(clock() - info::start) / CLOCKS_PER_SEC;
  float wst = float(info::allWords) / time;
  int eta = int(time / progress * (1 - progress) / args.thread);
  int etah = eta / 3600;
  int etam = (eta - etah * 3600) / 60;

  std::wcout << std::fixed;
  std::wcout << "\rProgress: " << std::setprecision(1) << 100 * progress << "%";
  std::wcout << "  words/sec/thread: " << std::setprecision(0) << wst;
  std::wcout << "  lr: " << std::setprecision(6) << model.getLearningRate();
  std::wcout << "  loss: " << std::setprecision(6) << avLoss;
  std::wcout << "  eta: " << etah << "h" << etam << "m  ";
  std::wcout << std::flush;
}

void supervised(Model& model,
                const std::vector<int32_t>& line,
                const std::vector<int32_t>& labels,
                double& loss, int32_t& N) {
    if (labels.size() == 0 || line.size() == 0) return;
    std::uniform_int_distribution<> uniform(0, labels.size() - 1);
    int32_t i = uniform(model.rng);
    model.update(line, labels[i], loss, N);
}

void cbow(Dictionary& dict, Model& model,
          const std::vector<int32_t>& line,
          double& loss, int32_t& N) {
  int32_t n = line.size();
  std::vector<int32_t> bow;
  std::uniform_int_distribution<> uniform(1, args.ws);
  for (int32_t w = 0; w < n; w++) {
    int32_t wb = uniform(model.rng);
    bow.clear();
    for (int32_t c = -wb; c <= wb; c++) {
      if (c != 0 && w + c >= 0 && w + c < n) {
        const std::vector<int32_t>& ngrams = dict.getNgrams(line[w + c]);
        for (auto it = ngrams.cbegin(); it != ngrams.cend(); ++it) {
          bow.push_back(*it);
        }
      }
    }
    model.update(bow, line[w], loss, N);
  }
}

void skipGram(Dictionary& dict, Model& model,
              const std::vector<int32_t>& line,
              double& loss, int32_t& N) {
  int32_t n = line.size();
  std::uniform_int_distribution<> uniform(1, args.ws);
  for (int32_t w = 0; w < n; w++) {
    int32_t wb = uniform(model.rng);
    const std::vector<int32_t>& ngrams = dict.getNgrams(line[w]);
    for (int32_t c = -wb; c <= wb; c++) {
      if (c != 0 && w + c >= 0 && w + c < n) {
        int32_t target = line[w + c];
        model.update(ngrams, target, loss, N);
      }
    }
  }
}

void test(Dictionary& dict, Model& model) {
  int32_t N = 0;
  double precision = 0.0;
  std::vector<int32_t> line, labels;
  std::wifstream ifs(args.test);
  while (!ifs.eof()) {
    dict.getLine(ifs, line, labels, model.rng);
    dict.addNgrams(line, args.wordNgrams);
    if (labels.size() > 0 && line.size() > 0) {
      int32_t i = model.predict(line);
      for (auto& t : labels) {
        if (i == t) {
          precision += 1.0;
          break;
        }
      }
      N++;
    }
  }
  ifs.close();
  std::wcout << std::setprecision(3) << "P@1: " << precision / N << std::endl;
  std::wcout << std::setprecision(3) << "Sentences: " << N << std::endl;
}

void thread_function(Dictionary& dict, Matrix& input, Matrix& output,
                     int32_t threadId) {
  std::wifstream ifs(args.input);
  utils::seek(ifs, threadId * utils::size(ifs) / args.thread);

  Model model(input, output, args.dim, args.lr, threadId);
  if (args.model == model_name::sup) {
    model.setLabelFreq(dict.getLabelFreq());
  } else {
    model.setLabelFreq(dict.getWordFreq());
  }

  const int64_t ntokens = dict.getNumTokens();
  int64_t tokenCount = 0;
  int64_t prevTokenCount = 0;
  double loss = 0.0;
  int32_t N = 0;
  std::vector<int32_t> line, labels;

  while (info::allWords < args.epoch * ntokens) {
    tokenCount += dict.getLine(ifs, line, labels, model.rng);
    if (args.model == model_name::sup) {
      dict.addNgrams(line, args.wordNgrams);
      supervised(model, line, labels, loss, N);
    } else if (args.model == model_name::cbow) {
      cbow(dict, model, line, loss, N);
    } else if (args.model == model_name::sg) {
      skipGram(dict, model, line, loss, N);
    }

    if (tokenCount - prevTokenCount > 10000) {
      info::allWords += tokenCount - prevTokenCount;
      prevTokenCount = tokenCount;
      info::allLoss += loss;
      info::allN += N;
      loss = 0.0;
      N = 0;
      real progress = real(info::allWords) / (args.epoch * ntokens);
      model.setLearningRate(args.lr * (1.0 - progress));
      if (threadId == 0) printInfo(model, ntokens);
    }
  }
  if (threadId == 0) {
    printInfo(model, ntokens);
    std::wcout << std::endl;
  }
  if (args.model == model_name::sup && threadId == 0) {
    test(dict, model);
  }
  ifs.close();
}

int main(int argc, char** argv) {
  std::locale::global(std::locale(""));
  args.parseArgs(argc, argv);
  utils::initTables();

  Dictionary dict;
  dict.readFromFile(args.input);

  Matrix input(dict.getNumWords() + args.bucket, args.dim);
  Matrix output;
  if (args.model == model_name::sup) {
    output = Matrix(dict.getNumLabels(), args.dim);
  } else {
    output = Matrix(dict.getNumWords(), args.dim);
  }
  input.uniform(1.0 / args.dim);
  output.zero();

  info::start = clock();

  std::vector<std::thread> threads;
  for (int32_t i = 0; i < args.thread; i++) {
    threads.push_back(std::thread(&thread_function, std::ref(dict),
                                  std::ref(input), std::ref(output), i));
  }
  for (auto it = threads.begin(); it != threads.end(); ++it) {
    it->join();
  }

  std::wcout << "training took: "
              << float(clock() - info::start) / CLOCKS_PER_SEC / args.thread
              << " s" << std::endl;

  if (args.output.size() != 0) {
    saveModel(dict, input, output);
    saveVectors(dict, input, output);
  }
  utils::freeTables();

  return 0;
}
<commit_msg>Remove unused variable & change long long to int64_t<commit_after>/**
 * Copyright (c) 2016-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#include "Matrix.h"
#include "Vector.h"
#include "Dictionary.h"
#include "Model.h"
#include "Utils.h"
#include "Real.h"
#include "Args.h"
#include <iostream>
#include <iomanip>
#include <thread>
#include <time.h>
#include <string>
#include <math.h>
#include <vector>
#include <atomic>
#include <fenv.h>

Args args;

namespace info {
  clock_t start;
  std::atomic<int64_t> allWords(0);
  std::atomic<int64_t> allN(0);
  double allLoss(0.0);
}

void saveVectors(Dictionary& dict, Matrix& input, Matrix& output) {
  int32_t N = dict.getNumWords();
  std::wofstream ofs(args.output + ".vec");
  if (ofs.is_open()) {
    ofs << N << ' ' << args.dim << std::endl;
    for (int32_t i = 0; i < N; i++) {
      ofs << dict.getWord(i) << ' ';
      Vector embedding(args.dim);
      embedding.zero();
      const std::vector<int32_t>& ngrams = dict.getNgrams(i);
      for (auto it = ngrams.begin(); it != ngrams.end(); ++it) {
        embedding.addRow(input, *it);
      }
      embedding.mul(1.0 / ngrams.size());
      embedding.writeToStream(ofs);
      ofs << std::endl;
    }
    ofs.close();
  } else {
    std::wcout << "Error opening file for writing" << std::endl;
  }
}

void saveModel(Dictionary& dict, Matrix& input, Matrix& output) {
  std::ofstream ofs(args.output + ".bin");
  args.save(ofs);
  dict.save(ofs);
  input.save(ofs);
  output.save(ofs);
  ofs.close();
}

void loadModel(Dictionary& dict, Matrix& input, Matrix& output) {
  std::ifstream ifs(args.output + ".bin");
  args.load(ifs);
  dict.load(ifs);
  input.load(ifs);
  output.load(ifs);
  ifs.close();
}

void printInfo(Model& model, int64_t numTokens) {
  real progress = real(info::allWords) / (args.epoch * numTokens);
  real avLoss = info::allLoss / info::allN;
  float time = float(clock() - info::start) / CLOCKS_PER_SEC;
  float wst = float(info::allWords) / time;
  int eta = int(time / progress * (1 - progress) / args.thread);
  int etah = eta / 3600;
  int etam = (eta - etah * 3600) / 60;

  std::wcout << std::fixed;
  std::wcout << "\rProgress: " << std::setprecision(1) << 100 * progress << "%";
  std::wcout << "  words/sec/thread: " << std::setprecision(0) << wst;
  std::wcout << "  lr: " << std::setprecision(6) << model.getLearningRate();
  std::wcout << "  loss: " << std::setprecision(6) << avLoss;
  std::wcout << "  eta: " << etah << "h" << etam << "m  ";
  std::wcout << std::flush;
}

void supervised(Model& model,
                const std::vector<int32_t>& line,
                const std::vector<int32_t>& labels,
                double& loss, int32_t& N) {
    if (labels.size() == 0 || line.size() == 0) return;
    std::uniform_int_distribution<> uniform(0, labels.size() - 1);
    int32_t i = uniform(model.rng);
    model.update(line, labels[i], loss, N);
}

void cbow(Dictionary& dict, Model& model,
          const std::vector<int32_t>& line,
          double& loss, int32_t& N) {
  int32_t n = line.size();
  std::vector<int32_t> bow;
  std::uniform_int_distribution<> uniform(1, args.ws);
  for (int32_t w = 0; w < n; w++) {
    int32_t wb = uniform(model.rng);
    bow.clear();
    for (int32_t c = -wb; c <= wb; c++) {
      if (c != 0 && w + c >= 0 && w + c < n) {
        const std::vector<int32_t>& ngrams = dict.getNgrams(line[w + c]);
        for (auto it = ngrams.cbegin(); it != ngrams.cend(); ++it) {
          bow.push_back(*it);
        }
      }
    }
    model.update(bow, line[w], loss, N);
  }
}

void skipGram(Dictionary& dict, Model& model,
              const std::vector<int32_t>& line,
              double& loss, int32_t& N) {
  int32_t n = line.size();
  std::uniform_int_distribution<> uniform(1, args.ws);
  for (int32_t w = 0; w < n; w++) {
    int32_t wb = uniform(model.rng);
    const std::vector<int32_t>& ngrams = dict.getNgrams(line[w]);
    for (int32_t c = -wb; c <= wb; c++) {
      if (c != 0 && w + c >= 0 && w + c < n) {
        int32_t target = line[w + c];
        model.update(ngrams, target, loss, N);
      }
    }
  }
}

void test(Dictionary& dict, Model& model) {
  int32_t N = 0;
  double precision = 0.0;
  std::vector<int32_t> line, labels;
  std::wifstream ifs(args.test);
  while (!ifs.eof()) {
    dict.getLine(ifs, line, labels, model.rng);
    dict.addNgrams(line, args.wordNgrams);
    if (labels.size() > 0 && line.size() > 0) {
      int32_t i = model.predict(line);
      for (auto& t : labels) {
        if (i == t) {
          precision += 1.0;
          break;
        }
      }
      N++;
    }
  }
  ifs.close();
  std::wcout << std::setprecision(3) << "P@1: " << precision / N << std::endl;
  std::wcout << std::setprecision(3) << "Sentences: " << N << std::endl;
}

void thread_function(Dictionary& dict, Matrix& input, Matrix& output,
                     int32_t threadId) {
  std::wifstream ifs(args.input);
  utils::seek(ifs, threadId * utils::size(ifs) / args.thread);

  Model model(input, output, args.dim, args.lr, threadId);
  if (args.model == model_name::sup) {
    model.setLabelFreq(dict.getLabelFreq());
  } else {
    model.setLabelFreq(dict.getWordFreq());
  }

  const int64_t ntokens = dict.getNumTokens();
  int64_t tokenCount = 0;
  double loss = 0.0;
  int32_t N = 0;
  std::vector<int32_t> line, labels;

  while (info::allWords < args.epoch * ntokens) {
    tokenCount += dict.getLine(ifs, line, labels, model.rng);
    if (args.model == model_name::sup) {
      dict.addNgrams(line, args.wordNgrams);
      supervised(model, line, labels, loss, N);
    } else if (args.model == model_name::cbow) {
      cbow(dict, model, line, loss, N);
    } else if (args.model == model_name::sg) {
      skipGram(dict, model, line, loss, N);
    }

    if (tokenCount > 10000) {
      info::allWords += tokenCount;
      info::allLoss += loss;
      info::allN += N;
      tokenCount = 0;
      loss = 0.0;
      N = 0;
      real progress = real(info::allWords) / (args.epoch * ntokens);
      model.setLearningRate(args.lr * (1.0 - progress));
      if (threadId == 0) printInfo(model, ntokens);
    }
  }
  if (threadId == 0) {
    printInfo(model, ntokens);
    std::wcout << std::endl;
  }
  if (args.model == model_name::sup && threadId == 0) {
    test(dict, model);
  }
  ifs.close();
}

int main(int argc, char** argv) {
  std::locale::global(std::locale(""));
  args.parseArgs(argc, argv);
  utils::initTables();

  Dictionary dict;
  dict.readFromFile(args.input);

  Matrix input(dict.getNumWords() + args.bucket, args.dim);
  Matrix output;
  if (args.model == model_name::sup) {
    output = Matrix(dict.getNumLabels(), args.dim);
  } else {
    output = Matrix(dict.getNumWords(), args.dim);
  }
  input.uniform(1.0 / args.dim);
  output.zero();

  info::start = clock();

  std::vector<std::thread> threads;
  for (int32_t i = 0; i < args.thread; i++) {
    threads.push_back(std::thread(&thread_function, std::ref(dict),
                                  std::ref(input), std::ref(output), i));
  }
  for (auto it = threads.begin(); it != threads.end(); ++it) {
    it->join();
  }

  float trainTime = float(clock() - info::start) / CLOCKS_PER_SEC / args.thread;
  std::wcout << "training took: " << trainTime << " sec" << std::endl;

  if (args.output.size() != 0) {
    saveModel(dict, input, output);
    saveVectors(dict, input, output);
  }
  utils::freeTables();

  return 0;
}
<|endoftext|>


<commit_before>#include "ScrollingMsg.hpp"
#include "utilities.hpp"
#include <iostream>
using std::cout;
using std::endl;

ScrollingMsg::ScrollingMsg()
  :m_current_w(0)
  ,m_current_h(0)
  ,m_friendly_name("None")
  ,m_loops(0)
  ,m_current_loop(0)
  ,m_scroll_time(12.0f)
  ,m_text()
{

};
ScrollingMsg::ScrollingMsg( const int width,
                            const int height,
                            const std::string& font,
                            const std::string& friendly_name,
                            const int& loop,
                            const std::string& msg,
                            const double& scroll_time,
                            const int& y_pos,
                            const bool dropshadow,
                            const bool underlay)
  :m_current_w(width)
  ,m_current_h(height)
  ,m_friendly_name(friendly_name)
  ,m_loops(loop)
  ,m_current_loop(0)
  ,m_scroll_time(scroll_time)
  ,m_text(msg, width, y_pos, font, dropshadow, underlay)
{

};

ScrollingMsg::~ScrollingMsg() {

}

void ScrollingMsg::Resize(const int width, const int height) {
  m_current_w = width;
  m_current_h = height;
};
bool ScrollingMsg::Update(const float dt)
{
  //d_pos = d/t * dt
  int xpos = m_text.X();
  xpos -= ((m_current_w + m_text.width())/m_scroll_time)*dt;
  if(xpos<(-1.0f*m_text.width())) {//wraparound
    xpos = m_current_w;
    m_current_loop += 1;
  }
  m_text.X(xpos);
  return (m_loops > 0 && m_current_loop >= m_loops);
};

void ScrollingMsg::Draw(cairo_t* context, const float dt)
{
  m_text.Draw(context, dt);
}

ScrollingMsgController::ScrollingMsgController()
{

}

void ScrollingMsgController::AddMsg(const int width,
  const int height,
  const std::string& font,
  const std::string& friendly_name,
  const int& loop,
  const std::string& msg,
  const double& scroll_time,
  const int& y_pos,
  const bool dropshadow,
  const bool underlay)
{
    m_msgs[friendly_name]=ScrollingMsg(width, height, font, friendly_name, loop, msg, scroll_time, y_pos, dropshadow, underlay);
}

void ScrollingMsgController::RemoveMsg(const std::string& friendly_name)
{
  std::map< std::string, ScrollingMsg >::iterator msg = m_msgs.find(friendly_name);
  if(msg!=m_msgs.end())
  {
    m_msgs.erase(msg);
  }
};

void ScrollingMsgController::Update(float dt) {
  for(std::map< std::string, ScrollingMsg >::iterator imsg=m_msgs.begin();
    imsg!=m_msgs.end();)
  {
    if(imsg->second.Update(dt)) {
      imsg = m_msgs.erase(imsg);
    }else{
      ++imsg;
    }
  }
};
void ScrollingMsgController::Draw(cairo_t* context, const float dt) {
  for(std::map< std::string, ScrollingMsg >::iterator imsg=m_msgs.begin();
    imsg!=m_msgs.end(); ++imsg)
  {
    imsg->second.Draw(context, dt);
  }
}

void ScrollingMsgController::Resize(const int width, const int height) {
  for(std::map< std::string, ScrollingMsg >::iterator imsg=m_msgs.begin();
    imsg!=m_msgs.end(); ++imsg)
  {
    imsg->second.Resize(width, height);
  }
}
<commit_msg>fixes #15<commit_after>#include "ScrollingMsg.hpp"
#include "utilities.hpp"
#include <iostream>
using std::cout;
using std::endl;

ScrollingMsg::ScrollingMsg()
  :m_current_w(0)
  ,m_current_h(0)
  ,m_friendly_name("None")
  ,m_loops(0)
  ,m_current_loop(0)
  ,m_scroll_time(12.0f)
  ,m_text()
{

};
ScrollingMsg::ScrollingMsg( const int width,
                            const int height,
                            const std::string& font,
                            const std::string& friendly_name,
                            const int& loop,
                            const std::string& msg,
                            const double& scroll_time,
                            const int& y_pos,
                            const bool dropshadow,
                            const bool underlay)
  :m_current_w(width)
  ,m_current_h(height)
  ,m_friendly_name(friendly_name)
  ,m_loops(loop)
  ,m_current_loop(0)
  ,m_scroll_time(scroll_time)
  ,m_text(msg, width, y_pos, font, dropshadow, underlay)
{

};

ScrollingMsg::~ScrollingMsg() {

}

void ScrollingMsg::Resize(const int width, const int height) {
  m_current_w = width;
  m_current_h = height;
};
bool ScrollingMsg::Update(const float dt)
{
  //d_pos = d/t * dt
  int xpos = m_text.X();
  xpos -= ((m_current_w + m_text.width())/m_scroll_time)*dt;
  if(xpos<(-1.0f*m_text.width())) {//wraparound
    xpos = m_current_w;
    m_current_loop += 1;
  }
  m_text.X(xpos);
  return (m_loops > 0 && m_current_loop > m_loops);
};

void ScrollingMsg::Draw(cairo_t* context, const float dt)
{
  m_text.Draw(context, dt);
}

ScrollingMsgController::ScrollingMsgController()
{

}

void ScrollingMsgController::AddMsg(const int width,
  const int height,
  const std::string& font,
  const std::string& friendly_name,
  const int& loop,
  const std::string& msg,
  const double& scroll_time,
  const int& y_pos,
  const bool dropshadow,
  const bool underlay)
{
    m_msgs[friendly_name]=ScrollingMsg(width, height, font, friendly_name, loop, msg, scroll_time, y_pos, dropshadow, underlay);
}

void ScrollingMsgController::RemoveMsg(const std::string& friendly_name)
{
  std::map< std::string, ScrollingMsg >::iterator msg = m_msgs.find(friendly_name);
  if(msg!=m_msgs.end())
  {
    m_msgs.erase(msg);
  }
};

void ScrollingMsgController::Update(float dt) {
  for(std::map< std::string, ScrollingMsg >::iterator imsg=m_msgs.begin();
    imsg!=m_msgs.end();)
  {
    if(imsg->second.Update(dt)) {
      imsg = m_msgs.erase(imsg);
    }else{
      ++imsg;
    }
  }
};
void ScrollingMsgController::Draw(cairo_t* context, const float dt) {
  for(std::map< std::string, ScrollingMsg >::iterator imsg=m_msgs.begin();
    imsg!=m_msgs.end(); ++imsg)
  {
    imsg->second.Draw(context, dt);
  }
}

void ScrollingMsgController::Resize(const int width, const int height) {
  for(std::map< std::string, ScrollingMsg >::iterator imsg=m_msgs.begin();
    imsg!=m_msgs.end(); ++imsg)
  {
    imsg->second.Resize(width, height);
  }
}
<|endoftext|>


<commit_before>#include "MemCheck.h"
#include "POGLRenderContext.h"
#include "POGLRenderState.h"
#include "POGLDevice.h"
#include "POGLEnum.h"
#include "POGLVertexBuffer.h"
#include "POGLIndexBuffer.h"
#include "POGLTexture2D.h"
#include "POGLShader.h"
#include "POGLProgramData.h"
#include "POGLFactory.h"
#include "POGLFramebuffer.h"
#include "POGLProgram.h"
#include <algorithm>

POGLRenderContext::POGLRenderContext(IPOGLDevice* device)
: mRenderState(nullptr), mDevice(device)
{
}

POGLRenderContext::~POGLRenderContext()
{
}

void POGLRenderContext::Destroy()
{
	POGL_SAFE_RELEASE(mRenderState);
}

IPOGLDevice* POGLRenderContext::GetDevice()
{
	mDevice->AddRef();
	return mDevice;
}

IPOGLShader* POGLRenderContext::CreateShaderFromFile(const POGL_CHAR* path, POGLShaderType::Enum type)
{
	POGL_ISTREAM stream(path);
	if (!stream.is_open())
		THROW_EXCEPTION(POGLResourceException, "Shader at path: '%s' could not be found", path);

	// Read the entire file into memory
	POGL_STRING str((std::istreambuf_iterator<POGL_CHAR>(stream)), std::istreambuf_iterator<POGL_CHAR>());
	return CreateShaderFromMemory(str.c_str(), str.length(), type);
}

IPOGLShader* POGLRenderContext::CreateShaderFromMemory(const POGL_CHAR* memory, POGL_UINT32 size, POGLShaderType::Enum type)
{
	// Generate a shader ID based on the supplied memory, size and type
	const GLuint shaderID = POGLFactory::CreateShader(memory, size, type);

	POGLShader* shader = new POGLShader(type);
	shader->PostConstruct(shaderID);
	return shader;
}

IPOGLProgram* POGLRenderContext::CreateProgramFromShaders(IPOGLShader** shaders)
{
	if (shaders == nullptr)
		THROW_EXCEPTION(POGLResourceException, "You must supply at least one shader to be able to create a program");

	// Attach all the shaders to the program
	const GLuint programID = POGLFactory::CreateProgram(shaders);
	POGLProgram* program = new POGLProgram();
	program->PostConstruct(programID, GetRenderState());
	return program;
}

IPOGLTexture1D* POGLRenderContext::CreateTexture1D()
{
	return nullptr;
}

IPOGLTexture2D* POGLRenderContext::CreateTexture2D(const POGL_SIZE& size, POGLTextureFormat::Enum format, const void* bytes)
{
	if (size.width <= 0)
		THROW_EXCEPTION(POGLResourceException, "You cannot create a texture with width: %d", size.width);

	if (size.height <= 0)
		THROW_EXCEPTION(POGLResourceException, "You cannot create a texture with height: %d", size.height);

	const GLenum _format = POGLEnum::ConvertToTextureFormatEnum(format);
	const GLenum _internalFormat = POGLEnum::ConvertToInternalTextureFormatEnum(format);
	const GLenum minFilter = POGLEnum::Convert(POGLMinFilter::DEFAULT);
	const GLenum magFilter = POGLEnum::Convert(POGLMagFilter::DEFAULT);
	const GLenum textureWrap = POGLEnum::Convert(POGLTextureWrap::DEFAULT);

	const GLuint textureID = POGLFactory::GenTextureID();
	glBindTexture(GL_TEXTURE_2D, textureID);

	glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, size.width, size.height, 0, _format, GL_UNSIGNED_BYTE, bytes);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textureWrap);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textureWrap);

	const GLenum status = glGetError();
	if (status != GL_NO_ERROR) {
		THROW_EXCEPTION(POGLResourceException, "Could not create 2D texture. Reason: 0x%x", status);
	}

	POGLTexture2D* texture = new POGLTexture2D(size, format);
	texture->PostConstruct(textureID);
	mRenderState->ForceSetTextureResource((POGLTextureResource*)texture->GetResourcePtr());
	return texture;
}

IPOGLTexture3D* POGLRenderContext::CreateTexture3D()
{
	return nullptr;
}

void POGLRenderContext::ResizeTexture2D(IPOGLTexture2D* texture, const POGL_SIZE& size)
{
	if (texture == nullptr)
		THROW_EXCEPTION(POGLStateException, "You cannot resize a non-existing texture");

	if (size.width <= 0)
		THROW_EXCEPTION(POGLStateException, "You cannot resize a texture to 0 width");

	if (size.height <= 0)
		THROW_EXCEPTION(POGLStateException, "You cannot resize a texture to 0 height");

	POGLTexture2D* impl = static_cast<POGLTexture2D*>(texture);
	POGLTextureResource* resource = impl->GetResourcePtr();
	mRenderState->BindTextureResource(resource, 0);

	const POGLTextureFormat::Enum format = resource->GetTextureFormat();
	const GLenum _format = POGLEnum::ConvertToTextureFormatEnum(format);
	const GLenum _internalFormat = POGLEnum::ConvertToInternalTextureFormatEnum(format);

	glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, size.width, size.height, 0, _format, GL_UNSIGNED_BYTE, NULL);
	impl->SetSize(size);
	CHECK_GL("Could not set new texture size");
}

IPOGLFramebuffer* POGLRenderContext::CreateFramebuffer(IPOGLTexture** textures)
{
	return CreateFramebuffer(textures, nullptr);
}

IPOGLFramebuffer* POGLRenderContext::CreateFramebuffer(IPOGLTexture** textures, IPOGLTexture* depthStencilTexture)
{
	//
	// Generate a framebuffer ID
	//

	const GLuint framebufferID = POGLFactory::GenFramebufferObjectID(textures, depthStencilTexture);

	//
	// Convert the textures array into a std::vector
	//

	std::vector<IPOGLTexture*> texturesVector;
	if (textures != nullptr) {
		for (IPOGLTexture** ptr = textures; *ptr != nullptr; ++ptr) {
			IPOGLTexture* texture = *ptr;
			texturesVector.push_back(texture);
		}
	}

	//
	// Create the actual object
	//

	POGLFramebuffer* framebuffer = new POGLFramebuffer(texturesVector, depthStencilTexture);
	framebuffer->PostConstruct(framebufferID);
	mRenderState->SetFramebuffer(framebuffer);
	return framebuffer;
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const void* memory, POGL_UINT32 memorySize, const POGL_VERTEX_LAYOUT* layout, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	if (memorySize == 0)
		THROW_EXCEPTION(POGLStateException, "You cannot create a non-existing vertex buffer");

	if (layout == nullptr)
		THROW_EXCEPTION(POGLStateException, "You cannot create a vertex buffer without a layout");

	const POGL_UINT32 numVertices = memorySize / layout->vertexSize;
	const GLenum usage = POGLEnum::Convert(bufferUsage);
	const GLenum type = POGLEnum::Convert(primitiveType);
	const GLuint bufferID = POGLFactory::GenBufferID();
	const GLuint vaoID = POGLFactory::GenVertexArrayObjectID(bufferID, layout);

	//
	// Create the object
	//

	POGLVertexBuffer* vb = new POGLVertexBuffer(numVertices, layout, type, usage);
	vb->PostConstruct(bufferID, vaoID);

	//
	// Fill the buffer with data
	//

	glBufferData(GL_ARRAY_BUFFER, memorySize, memory, usage);

	//
	// Make sure to mark this buffer as the current vertex buffer
	//

	mRenderState->ForceSetVertexBuffer(vb);

	const GLenum error = glGetError();
	if (error != GL_NO_ERROR)
		THROW_EXCEPTION(POGLResourceException, "Failed to create a vertex buffer. Reason: 0x%x", error);

	// Finished
	return vb;
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const POGL_POSITION_VERTEX* memory, POGL_UINT32 memorySize, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	return CreateVertexBuffer(memory, memorySize, &POGL_POSITION_VERTEX_LAYOUT, primitiveType, bufferUsage);
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const POGL_POSITION_COLOR_VERTEX* memory, POGL_UINT32 memorySize, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	return CreateVertexBuffer(memory, memorySize, &POGL_POSITION_COLOR_VERTEX_LAYOUT, primitiveType, bufferUsage);
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const POGL_POSITION_TEXCOORD_VERTEX* memory, POGL_UINT32 memorySize, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	return CreateVertexBuffer(memory, memorySize, &POGL_POSITION_TEXCOORD_VERTEX_LAYOUT, primitiveType, bufferUsage);
}

IPOGLIndexBuffer* POGLRenderContext::CreateIndexBuffer(const void* memory, POGL_UINT32 memorySize, POGLVertexType::Enum type, POGLBufferUsage::Enum bufferUsage)
{
	if (memorySize == 0)
		THROW_EXCEPTION(POGLStateException, "You cannot create a non-existing index buffer");

	if (type == POGLVertexType::FLOAT || type == POGLVertexType::DOUBLE)
		THROW_EXCEPTION(POGLStateException, "You are not allowed to create an index buffer of a decimal type");

	const POGL_UINT32 typeSize = POGLEnum::VertexTypeSize(type);
	const POGL_UINT32 numIndices = memorySize / typeSize;
	const GLuint bufferID = POGLFactory::GenBufferID();
	const GLenum usage = POGLEnum::Convert(bufferUsage);
	const GLenum indiceType = POGLEnum::Convert(type);

	//
	// Fill the buffer with data
	//

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID);
	if (memory != nullptr)
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, memorySize, memory, usage);

	POGLIndexBuffer* ib = new POGLIndexBuffer(typeSize, numIndices, indiceType, usage);
	ib->PostConstruct(bufferID);

	//
	// Make sure to mark this buffer as the current index buffer
	//

	mRenderState->ForceSetIndexBuffer(ib);

	const GLenum error = glGetError();
	if (error != GL_NO_ERROR)
		THROW_EXCEPTION(POGLResourceException, "Failed to create a index buffer. Reason: 0x%x", error);

	// Finished
	return ib;
}

IPOGLResource* POGLRenderContext::CloneResource(IPOGLResource* resource)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::CopyResource(IPOGLResource* source, IPOGLResource* destination)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::CopyResource(IPOGLResource* source, IPOGLResource* destination, POGL_UINT32 sourceOffset, POGL_UINT32 destinationOffset, POGL_UINT32 size)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

IPOGLRenderState* POGLRenderContext::Apply(IPOGLProgram* program)
{
	if (program == nullptr)
		THROW_EXCEPTION(POGLResourceException, "You are not allowed to apply a non-existing program");

	mRenderState->Apply(static_cast<POGLProgram*>(program));
	mRenderState->AddRef();
	return mRenderState;
}

void* POGLRenderContext::Map(IPOGLResource* resource, POGLResourceMapType::Enum e)
{
	auto type = resource->GetType();
	if (type == POGLResourceType::VERTEXBUFFER) {
		POGLVertexBuffer* vb = static_cast<POGLVertexBuffer*>(resource);
		mRenderState->BindVertexBuffer(vb);
		return glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
	}

	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void* POGLRenderContext::Map(IPOGLResource* resource, POGL_UINT32 offset, POGL_UINT32 length, POGLResourceMapType::Enum e)
{
	auto type = resource->GetType();
	if (type == POGLResourceType::VERTEXBUFFER) {
		POGLVertexBuffer* vb = static_cast<POGLVertexBuffer*>(resource);
		const POGL_UINT32 memorySize = vb->GetCount() * vb->GetLayout()->vertexSize;
		if (offset + length > memorySize)
			THROW_EXCEPTION(POGLStateException, "You cannot map with offset: %d and length: %d when the vertex buffer size is: %d", offset, length, memorySize);

		mRenderState->BindVertexBuffer(vb);
		return glMapBufferRange(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
	}

	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::Unmap(IPOGLResource* resource)
{
	auto type = resource->GetType();
	if (type == POGLResourceType::VERTEXBUFFER) {
		glUnmapBuffer(GL_ARRAY_BUFFER);
		return;
	}

	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::SetViewport(const POGL_RECT& viewport)
{
	mRenderState->SetViewport(viewport);
}

void POGLRenderContext::InitializeRenderState()
{
	if (mRenderState == nullptr) {
		mRenderState = new POGLRenderState(this);
	}
}
<commit_msg>No need to bind the index buffer unless we want to save data to it<commit_after>#include "MemCheck.h"
#include "POGLRenderContext.h"
#include "POGLRenderState.h"
#include "POGLDevice.h"
#include "POGLEnum.h"
#include "POGLVertexBuffer.h"
#include "POGLIndexBuffer.h"
#include "POGLTexture2D.h"
#include "POGLShader.h"
#include "POGLProgramData.h"
#include "POGLFactory.h"
#include "POGLFramebuffer.h"
#include "POGLProgram.h"
#include <algorithm>

POGLRenderContext::POGLRenderContext(IPOGLDevice* device)
: mRenderState(nullptr), mDevice(device)
{
}

POGLRenderContext::~POGLRenderContext()
{
}

void POGLRenderContext::Destroy()
{
	POGL_SAFE_RELEASE(mRenderState);
}

IPOGLDevice* POGLRenderContext::GetDevice()
{
	mDevice->AddRef();
	return mDevice;
}

IPOGLShader* POGLRenderContext::CreateShaderFromFile(const POGL_CHAR* path, POGLShaderType::Enum type)
{
	POGL_ISTREAM stream(path);
	if (!stream.is_open())
		THROW_EXCEPTION(POGLResourceException, "Shader at path: '%s' could not be found", path);

	// Read the entire file into memory
	POGL_STRING str((std::istreambuf_iterator<POGL_CHAR>(stream)), std::istreambuf_iterator<POGL_CHAR>());
	return CreateShaderFromMemory(str.c_str(), str.length(), type);
}

IPOGLShader* POGLRenderContext::CreateShaderFromMemory(const POGL_CHAR* memory, POGL_UINT32 size, POGLShaderType::Enum type)
{
	// Generate a shader ID based on the supplied memory, size and type
	const GLuint shaderID = POGLFactory::CreateShader(memory, size, type);

	POGLShader* shader = new POGLShader(type);
	shader->PostConstruct(shaderID);
	return shader;
}

IPOGLProgram* POGLRenderContext::CreateProgramFromShaders(IPOGLShader** shaders)
{
	if (shaders == nullptr)
		THROW_EXCEPTION(POGLResourceException, "You must supply at least one shader to be able to create a program");

	// Attach all the shaders to the program
	const GLuint programID = POGLFactory::CreateProgram(shaders);
	POGLProgram* program = new POGLProgram();
	program->PostConstruct(programID, GetRenderState());
	return program;
}

IPOGLTexture1D* POGLRenderContext::CreateTexture1D()
{
	return nullptr;
}

IPOGLTexture2D* POGLRenderContext::CreateTexture2D(const POGL_SIZE& size, POGLTextureFormat::Enum format, const void* bytes)
{
	if (size.width <= 0)
		THROW_EXCEPTION(POGLResourceException, "You cannot create a texture with width: %d", size.width);

	if (size.height <= 0)
		THROW_EXCEPTION(POGLResourceException, "You cannot create a texture with height: %d", size.height);

	const GLenum _format = POGLEnum::ConvertToTextureFormatEnum(format);
	const GLenum _internalFormat = POGLEnum::ConvertToInternalTextureFormatEnum(format);
	const GLenum minFilter = POGLEnum::Convert(POGLMinFilter::DEFAULT);
	const GLenum magFilter = POGLEnum::Convert(POGLMagFilter::DEFAULT);
	const GLenum textureWrap = POGLEnum::Convert(POGLTextureWrap::DEFAULT);

	const GLuint textureID = POGLFactory::GenTextureID();
	glBindTexture(GL_TEXTURE_2D, textureID);

	glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, size.width, size.height, 0, _format, GL_UNSIGNED_BYTE, bytes);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textureWrap);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textureWrap);

	const GLenum status = glGetError();
	if (status != GL_NO_ERROR) {
		THROW_EXCEPTION(POGLResourceException, "Could not create 2D texture. Reason: 0x%x", status);
	}

	POGLTexture2D* texture = new POGLTexture2D(size, format);
	texture->PostConstruct(textureID);
	mRenderState->ForceSetTextureResource((POGLTextureResource*)texture->GetResourcePtr());
	return texture;
}

IPOGLTexture3D* POGLRenderContext::CreateTexture3D()
{
	return nullptr;
}

void POGLRenderContext::ResizeTexture2D(IPOGLTexture2D* texture, const POGL_SIZE& size)
{
	if (texture == nullptr)
		THROW_EXCEPTION(POGLStateException, "You cannot resize a non-existing texture");

	if (size.width <= 0)
		THROW_EXCEPTION(POGLStateException, "You cannot resize a texture to 0 width");

	if (size.height <= 0)
		THROW_EXCEPTION(POGLStateException, "You cannot resize a texture to 0 height");

	POGLTexture2D* impl = static_cast<POGLTexture2D*>(texture);
	POGLTextureResource* resource = impl->GetResourcePtr();
	mRenderState->BindTextureResource(resource, 0);

	const POGLTextureFormat::Enum format = resource->GetTextureFormat();
	const GLenum _format = POGLEnum::ConvertToTextureFormatEnum(format);
	const GLenum _internalFormat = POGLEnum::ConvertToInternalTextureFormatEnum(format);

	glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, size.width, size.height, 0, _format, GL_UNSIGNED_BYTE, NULL);
	impl->SetSize(size);
	CHECK_GL("Could not set new texture size");
}

IPOGLFramebuffer* POGLRenderContext::CreateFramebuffer(IPOGLTexture** textures)
{
	return CreateFramebuffer(textures, nullptr);
}

IPOGLFramebuffer* POGLRenderContext::CreateFramebuffer(IPOGLTexture** textures, IPOGLTexture* depthStencilTexture)
{
	//
	// Generate a framebuffer ID
	//

	const GLuint framebufferID = POGLFactory::GenFramebufferObjectID(textures, depthStencilTexture);

	//
	// Convert the textures array into a std::vector
	//

	std::vector<IPOGLTexture*> texturesVector;
	if (textures != nullptr) {
		for (IPOGLTexture** ptr = textures; *ptr != nullptr; ++ptr) {
			IPOGLTexture* texture = *ptr;
			texturesVector.push_back(texture);
		}
	}

	//
	// Create the actual object
	//

	POGLFramebuffer* framebuffer = new POGLFramebuffer(texturesVector, depthStencilTexture);
	framebuffer->PostConstruct(framebufferID);
	mRenderState->SetFramebuffer(framebuffer);
	return framebuffer;
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const void* memory, POGL_UINT32 memorySize, const POGL_VERTEX_LAYOUT* layout, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	if (memorySize == 0)
		THROW_EXCEPTION(POGLStateException, "You cannot create a non-existing vertex buffer");

	if (layout == nullptr)
		THROW_EXCEPTION(POGLStateException, "You cannot create a vertex buffer without a layout");

	const POGL_UINT32 numVertices = memorySize / layout->vertexSize;
	const GLenum usage = POGLEnum::Convert(bufferUsage);
	const GLenum type = POGLEnum::Convert(primitiveType);
	const GLuint bufferID = POGLFactory::GenBufferID();
	const GLuint vaoID = POGLFactory::GenVertexArrayObjectID(bufferID, layout);

	//
	// Create the object
	//

	POGLVertexBuffer* vb = new POGLVertexBuffer(numVertices, layout, type, usage);
	vb->PostConstruct(bufferID, vaoID);

	//
	// Fill the buffer with data
	//

	glBufferData(GL_ARRAY_BUFFER, memorySize, memory, usage);

	//
	// Make sure to mark this buffer as the current vertex buffer
	//

	mRenderState->ForceSetVertexBuffer(vb);

	const GLenum error = glGetError();
	if (error != GL_NO_ERROR)
		THROW_EXCEPTION(POGLResourceException, "Failed to create a vertex buffer. Reason: 0x%x", error);

	// Finished
	return vb;
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const POGL_POSITION_VERTEX* memory, POGL_UINT32 memorySize, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	return CreateVertexBuffer(memory, memorySize, &POGL_POSITION_VERTEX_LAYOUT, primitiveType, bufferUsage);
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const POGL_POSITION_COLOR_VERTEX* memory, POGL_UINT32 memorySize, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	return CreateVertexBuffer(memory, memorySize, &POGL_POSITION_COLOR_VERTEX_LAYOUT, primitiveType, bufferUsage);
}

IPOGLVertexBuffer* POGLRenderContext::CreateVertexBuffer(const POGL_POSITION_TEXCOORD_VERTEX* memory, POGL_UINT32 memorySize, POGLPrimitiveType::Enum primitiveType, POGLBufferUsage::Enum bufferUsage)
{
	return CreateVertexBuffer(memory, memorySize, &POGL_POSITION_TEXCOORD_VERTEX_LAYOUT, primitiveType, bufferUsage);
}

IPOGLIndexBuffer* POGLRenderContext::CreateIndexBuffer(const void* memory, POGL_UINT32 memorySize, POGLVertexType::Enum type, POGLBufferUsage::Enum bufferUsage)
{
	if (memorySize == 0)
		THROW_EXCEPTION(POGLStateException, "You cannot create a non-existing index buffer");

	if (type == POGLVertexType::FLOAT || type == POGLVertexType::DOUBLE)
		THROW_EXCEPTION(POGLStateException, "You are not allowed to create an index buffer of a decimal type");

	const POGL_UINT32 typeSize = POGLEnum::VertexTypeSize(type);
	const POGL_UINT32 numIndices = memorySize / typeSize;
	const GLuint bufferID = POGLFactory::GenBufferID();
	const GLenum usage = POGLEnum::Convert(bufferUsage);
	const GLenum indiceType = POGLEnum::Convert(type);

	POGLIndexBuffer* ib = new POGLIndexBuffer(typeSize, numIndices, indiceType, usage);
	ib->PostConstruct(bufferID);

	//
	// Fill the buffer with data
	//

	if (memory != nullptr) {
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, memorySize, memory, usage);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID);

		//
		// Make sure to mark this buffer as the current index buffer
		//

		mRenderState->ForceSetIndexBuffer(ib);

	}

	const GLenum error = glGetError();
	if (error != GL_NO_ERROR)
		THROW_EXCEPTION(POGLResourceException, "Failed to create a index buffer. Reason: 0x%x", error);

	// Finished
	return ib;
}

IPOGLResource* POGLRenderContext::CloneResource(IPOGLResource* resource)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::CopyResource(IPOGLResource* source, IPOGLResource* destination)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::CopyResource(IPOGLResource* source, IPOGLResource* destination, POGL_UINT32 sourceOffset, POGL_UINT32 destinationOffset, POGL_UINT32 size)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

IPOGLRenderState* POGLRenderContext::Apply(IPOGLProgram* program)
{
	if (program == nullptr)
		THROW_EXCEPTION(POGLResourceException, "You are not allowed to apply a non-existing program");

	mRenderState->Apply(static_cast<POGLProgram*>(program));
	mRenderState->AddRef();
	return mRenderState;
}

void* POGLRenderContext::Map(IPOGLResource* resource, POGLResourceMapType::Enum e)
{
	auto type = resource->GetType();
	if (type == POGLResourceType::VERTEXBUFFER) {
		POGLVertexBuffer* vb = static_cast<POGLVertexBuffer*>(resource);
		mRenderState->BindVertexBuffer(vb);
		return glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
	}

	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void* POGLRenderContext::Map(IPOGLResource* resource, POGL_UINT32 offset, POGL_UINT32 length, POGLResourceMapType::Enum e)
{
	auto type = resource->GetType();
	if (type == POGLResourceType::VERTEXBUFFER) {
		POGLVertexBuffer* vb = static_cast<POGLVertexBuffer*>(resource);
		const POGL_UINT32 memorySize = vb->GetCount() * vb->GetLayout()->vertexSize;
		if (offset + length > memorySize)
			THROW_EXCEPTION(POGLStateException, "You cannot map with offset: %d and length: %d when the vertex buffer size is: %d", offset, length, memorySize);

		mRenderState->BindVertexBuffer(vb);
		return glMapBufferRange(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
	}

	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::Unmap(IPOGLResource* resource)
{
	auto type = resource->GetType();
	if (type == POGLResourceType::VERTEXBUFFER) {
		glUnmapBuffer(GL_ARRAY_BUFFER);
		return;
	}

	THROW_NOT_IMPLEMENTED_EXCEPTION();
}

void POGLRenderContext::SetViewport(const POGL_RECT& viewport)
{
	mRenderState->SetViewport(viewport);
}

void POGLRenderContext::InitializeRenderState()
{
	if (mRenderState == nullptr) {
		mRenderState = new POGLRenderState(this);
	}
}
<|endoftext|>


<commit_before><commit_msg>Basic student file for homeworks. <commit_after><|endoftext|>


<commit_before>/***********************************************
 * Author: Jun Jiang - jiangjun4@sina.com
 * Create: 2017-08-19 18:50
 * Last modified : 2017-08-19 18:50
 * Filename      : DNN.cpp
 * Description   :
 **********************************************/
#include "dnn/DNN.h"
#include "dnn/Layer.h"
#include "utils/Log.h"
#include "utils/Shuffler.h"
#include <vector>

namespace abcdl{
namespace dnn{

void DNN::train(const abcdl::algebra::Mat& train_data,
                const abcdl::algebra::Mat& train_label,
                const abcdl::algebra::Mat& test_data,
                const abcdl::algebra::Mat& test_label){
    LOG(INFO) << "dnn start training...";

    size_t layer_size = _layers.size();
    size_t num_train_data = train_data.rows();

    CHECK(num_train_data == train_label.rows());
    CHECK(train_data.cols() == _layers[0]->get_input_dim());
    CHECK(train_label.cols() == _layers[_layers.size() -1]->get_output_dim());

    abcdl::utils::Shuffler shuffler(num_train_data);
    abcdl::algebra::Mat data;
    abcdl::algebra::Mat label;

    for(size_t i = 0; i != _epoch; i++){
        shuffler.shuffle();
        for(size_t j = 0; j != num_train_data; j++){
            train_data.get_row(&data, shuffler.get_row(j));
            train_label.get_row(&label, shuffler.get_row(j));

            for(size_t k = 0; k != layer_size; k++){
                if(k == 0){
                    ((InputLayer*)_layers[k])->set_x(data);
                }
                _layers[k]->forward(_layers[k-1]);
            }
            //printf("[%ld][%ld]\n", j, num_train_data);

            for(size_t k = layer_size - 1; k > 0; k--){
                if(k == layer_size - 1){
                    ((OutputLayer*)_layers[k])->set_y(label);
                    _layers[k]->backward(_layers[k-1], nullptr);
                }else{
                    _layers[k]->backward(_layers[k-1], _layers[k+1]);
                }

                //mini_batch_update
                if(j % _batch_size == _batch_size - 1 || j == num_train_data - 1){
                    _layers[k]->update_gradient(j % _batch_size + 1, _alpha, _lamda);
                }
            }

            if(j % 100 == 0){
                printf("Epoch[%ld/%ld] Train[%ld/%ld]\r", i + 1, _epoch, j, num_train_data);
            }
        }

        if(test_data.rows() > 0){
            size_t num = evaluate(test_data, test_label);
            printf("Epoch[%ld][%ld/%ld] rate[%f]\n", i + 1, num, test_data.rows(), num/(real)test_data.rows());
        }
    }
}

size_t DNN::evaluate(const abcdl::algebra::Mat& test_data, const abcdl::algebra::Mat& test_label){
    CHECK(test_data.cols() == _layers[0]->get_input_dim());
    CHECK(test_data.rows() == test_label.rows());
    CHECK(test_label.cols() == _layers[_layers.size() - 1]->get_output_dim());

    size_t rows = test_data.rows();
    size_t predict_num = 0;
    abcdl::algebra::Mat mat;

    for(size_t i = 0; i != rows; i++){
        predict(mat, test_data.get_row(i));
        size_t p_label = mat.argmax();
        size_t t_label = test_label.get_row(i).argmax();
        if(p_label == t_label){
            ++predict_num;
        }else{
            printf("[%ld][%f][%ld]\n", p_label, mat.max(), t_label);
        }
    }
    return predict_num;
}

void DNN::predict(abcdl::algebra::Mat& result, const abcdl::algebra::Mat& predict_data){
    CHECK(predict_data.cols() == _layers[0]->get_input_dim());
    size_t layer_size = _layers.size();
    for(size_t k = 0; k != layer_size; k++){
        if(k == 0){
            ((InputLayer*)_layers[k])->set_x(predict_data);
        }
        _layers[k]->forward(_layers[k-1]);
    }
    result = _layers[layer_size - 1]->get_activate_data();
}

bool DNN::load_model(const std::string& path){
    std::vector<abcdl::algebra::Mat*> models;
    if(!model_loader.read<real>(path, &models, "DNNMODEL") || models.size() % 2 != 0){
        for(auto& model : models){
            delete model;
        }
        models.clear();
        return false;
    }

    for(auto& layer : _layers){
        delete layer;
    }
    _layers.clear();

    for(size_t i = 0; i != models.size() / 2; i++){
        if(i == 0){
            _layers.push_back(new InputLayer(models[0]->rows()));
        }
        if(i == models.size() / 2 - 1){
            _layers.push_back(new OutputLayer(models[i * 2]->rows(), models[i * 2]->cols(), new SigmoidActivateFunc(), new CrossEntropyCost(), *models[i * 2], *models[i * 2 + 1]));
        }else{
            _layers.push_back(new FullConnLayer(models[i * 2]->rows(), models[i * 2]->cols(), new SigmoidActivateFunc(), *models[i * 2], *models[i * 2 + 1]));
        }
    }

    for(auto& model : models){
        delete model;
    }
    models.clear();
    return true;
}
bool DNN::write_model(const std::string& path){
    std::vector<abcdl::algebra::Mat*> models;
    for(size_t i = 1; i != _layers.size(); i++){
        models.push_back(&_layers[i]->get_weight());
        models.push_back(&_layers[i]->get_bias());
    }
    return model_loader.write<real>(models, path, "DNNMODEL", false);
}

}//namespace dnn
}//namespace abcdl
<commit_msg>initialized<commit_after>/***********************************************
 * Author: Jun Jiang - jiangjun4@sina.com
 * Create: 2017-08-19 18:50
 * Last modified : 2017-08-19 18:50
 * Filename      : DNN.cpp
 * Description   :
 **********************************************/
#include "dnn/DNN.h"
#include "dnn/Layer.h"
#include "utils/Log.h"
#include "utils/Shuffler.h"
#include <vector>

namespace abcdl{
namespace dnn{

void DNN::train(const abcdl::algebra::Mat& train_data,
                const abcdl::algebra::Mat& train_label,
                const abcdl::algebra::Mat& test_data,
                const abcdl::algebra::Mat& test_label){
    LOG(INFO) << "dnn start training...";

    size_t layer_size = _layers.size();
    size_t num_train_data = train_data.rows();

    CHECK(num_train_data == train_label.rows());
    CHECK(train_data.cols() == _layers[0]->get_input_dim());
    CHECK(train_label.cols() == _layers[_layers.size() -1]->get_output_dim());

    abcdl::utils::Shuffler shuffler(num_train_data);
    abcdl::algebra::Mat data;
    abcdl::algebra::Mat label;

    for(size_t i = 0; i != _epoch; i++){
        shuffler.shuffle();
        for(size_t j = 0; j != num_train_data; j++){
            train_data.get_row(&data, shuffler.get_row(j));
            train_label.get_row(&label, shuffler.get_row(j));

            for(size_t k = 0; k != layer_size; k++){
                if(k == 0){
                    ((InputLayer*)_layers[k])->set_x(data);
                }
                _layers[k]->forward(_layers[k-1]);
            }

            for(size_t k = layer_size - 1; k > 0; k--){
                if(k == layer_size - 1){
                    ((OutputLayer*)_layers[k])->set_y(label);
                    _layers[k]->backward(_layers[k-1], nullptr);
                }else{
                    _layers[k]->backward(_layers[k-1], _layers[k+1]);
                }

                //mini_batch_update
                if(j % _batch_size == _batch_size - 1 || j == num_train_data - 1){
                    _layers[k]->update_gradient(j % _batch_size + 1, _alpha, _lamda);
                }
            }

            if(j % 100 == 0){
                printf("Epoch[%ld/%ld] Train[%ld/%ld]\r", i + 1, _epoch, j, num_train_data);
            }
        }

        if(test_data.rows() > 0){
            size_t num = evaluate(test_data, test_label);
            printf("Epoch[%ld][%ld/%ld] rate[%f]\n", i + 1, num, test_data.rows(), num/(real)test_data.rows());
        }
    }
}

size_t DNN::evaluate(const abcdl::algebra::Mat& test_data, const abcdl::algebra::Mat& test_label){
    CHECK(test_data.cols() == _layers[0]->get_input_dim());
    CHECK(test_data.rows() == test_label.rows());
    CHECK(test_label.cols() == _layers[_layers.size() - 1]->get_output_dim());

    size_t rows = test_data.rows();
    size_t predict_num = 0;
    abcdl::algebra::Mat mat;

    for(size_t i = 0; i != rows; i++){
        predict(mat, test_data.get_row(i));
        if(mat.argmax() == test_label.get_row(i).argmax()){
            ++predict_num;
        }
    }
    return predict_num;
}

void DNN::predict(abcdl::algebra::Mat& result, const abcdl::algebra::Mat& predict_data){
    CHECK(predict_data.cols() == _layers[0]->get_input_dim());
    size_t layer_size = _layers.size();
    for(size_t k = 0; k != layer_size; k++){
        if(k == 0){
            ((InputLayer*)_layers[k])->set_x(predict_data);
        }
        _layers[k]->forward(_layers[k-1]);
    }
    result = _layers[layer_size - 1]->get_activate_data();
}

bool DNN::load_model(const std::string& path){
    std::vector<abcdl::algebra::Mat*> models;
    if(!model_loader.read<real>(path, &models, "DNNMODEL") || models.size() % 2 != 0){
        for(auto& model : models){
            delete model;
        }
        models.clear();
        return false;
    }

    for(auto& layer : _layers){
        delete layer;
    }
    _layers.clear();

    for(size_t i = 0; i != models.size() / 2; i++){
        if(i == 0){
            _layers.push_back(new InputLayer(models[0]->rows()));
        }
        if(i == models.size() / 2 - 1){
            _layers.push_back(new OutputLayer(models[i * 2]->rows(), models[i * 2]->cols(), new SigmoidActivateFunc(), new CrossEntropyCost(), *models[i * 2], *models[i * 2 + 1]));
        }else{
            _layers.push_back(new FullConnLayer(models[i * 2]->rows(), models[i * 2]->cols(), new SigmoidActivateFunc(), *models[i * 2], *models[i * 2 + 1]));
        }
    }

    for(auto& model : models){
        delete model;
    }
    models.clear();
    return true;
}
bool DNN::write_model(const std::string& path){
    std::vector<abcdl::algebra::Mat*> models;
    for(size_t i = 1; i != _layers.size(); i++){
        models.push_back(&_layers[i]->get_weight());
        models.push_back(&_layers[i]->get_bias());
    }
    return model_loader.write<real>(models, path, "DNNMODEL", false);
}

}//namespace dnn
}//namespace abcdl
<|endoftext|>


<commit_before>#include <iostream>

#include "macho/MachoBinary.h"
#include "macho/FatBinary.h"
#include "AbstractBinary.h"
#include "macho/MachoBinaryVisitor.h"

using namespace std;

// Que quiero hacer?
// Principalmente quiero que la libreria sea flexible para que pueda ser utilizada para varias cosas.
// En particular, para hacer una tool de inspeccion de mach-o y tambien como un parser de mach-o para
// el disassembler. Los dos escenarios son propicios para implementar algo tipo observer, donde cada
// load command tiene su handler. El problema llega cuando queremos informacion de las sections. Que
// es lo que hacemos? Las sections estan dentro de un segment o segment_64. Para llamar a los handlers
// de las sections deberiamos tener una forma de inspeccionar los segment's.
class MachoBinaryPrinterVisitor: public MachoBinaryVisitor {
public:
    virtual ~MachoBinaryPrinterVisitor() = default;

    virtual bool handle_mach_header(const struct mach_header &header) override {
    	LOG_DEBUG("CACA -> %s", __PRETTY_FUNCTION__);
    	return true;
    }

    virtual bool handle_mach_header(const struct mach_header_64 &header) override {
    	LOG_DEBUG("CACA -> %s", __PRETTY_FUNCTION__);
    	return true;
    }
};

int main(int argc, char **argv) {
	MachoBinary binary { new MachoBinaryPrinterVisitor() };
	if (!binary.load(argv[1])) {
		cerr << "Could not open mach-o binary" << endl;
		return -1;
	}

	if (!binary.init()) {
		cerr << "Could not initialize mach-o binary" << endl;
		binary.unload();
		return -1;
	}

	cout << "Welcome to reTools!" << endl;
	return 0;

	if (argc < 2) {
		cerr << "I need a binary to analyze" << endl;
		return -1;
	}

	FatBinary fat_binary { };
	if (!fat_binary.load(argv[1])) {
		cerr << "Could not open mach-o binary" << endl;
		return -1;
	}

	if (!fat_binary.init()) {
		cerr << "Could not initialize mach-o binary" << endl;
		fat_binary.unload();
		return -1;
	}

	// For each of the underlying binaries.
	for (AbstractBinary *sub_binary : fat_binary.binaries()) {
	}

	return 0;
}
<commit_msg>Not needed anymore.<commit_after><|endoftext|>