2.開発の事前準備
2.1 命令集チェック
// システムがサポートする命令集を確認する
// FMA/AVXパラメーターを用いてruntime_check.exeを実行させます
// 返り値は"true"の場合は, FMA/AVXは利用可能です。
static bool runtime_check(bool *fma, bool *avx) {
if (!fma || !avx) {
return false;
}
*fma = true;
*avx = true;
char exe[] = "runtime_check.exe ";
bool is_exist = false;
if (_access(exe, 0) == 0) {
is_exist = true;
}
if (is_exist) {
char arg[2][4] = { "FMA", "AVX" };
PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags |= STARTF_USESTDHANDLES; // STARTF_USESTDHANDLES is Required.
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
BOOL bSuccess;
int i;
for (i = 0; i < 2; i++) {
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
HANDLE hChildStdoutRd = NULL;
HANDLE hChildStdoutWr = NULL;
if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
printf("cannot create pipe\n");
return false;
}
// Ensure the read handle to the pipe for STDOUT is not inherited.
if (!SetHandleInformation(hChildStdoutRd, HANDLE_FLAG_INHERIT, 0)) {
printf("Stdout SetHandleInformation");
return false;
}
si.hStdOutput = hChildStdoutWr;
si.hStdError = hChildStdoutWr;
// Requires STARTF_USESTDHANDLES in dwFlags
// Requires STARTF_USESTDHANDLES in dwFlags
char *cmd = new char[strlen(exe) + strlen(arg[i]) + 1];
strcpy(cmd, exe);
strcat(cmd, arg[i]);
bSuccess = CreateProcess(
NULL, // No module name (use command line)
cmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to TRUE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi); // Pointer to PROCESS_INFORMATION structure
delete[] cmd;
if (bSuccess) {
if (0 == i) *fma = false;
if (1 == i) *avx = false;
WaitForSingleObject(pi.hProcess, INFINITE);
if (!CloseHandle(hChildStdoutWr)) {
printf("cannot close handle");
return false;
}
std::string strResult;
// Read output from the child process.
for (;;) {
DWORD dwRead;
char chBuf[64];
// Read from pipe that is the standard output for child process.
bSuccess = ReadFile(hChildStdoutRd, chBuf, 64, &dwRead, NULL);
if (!bSuccess || 0 == dwRead) {
break;
}
strResult += std::string(chBuf, dwRead);
}
if (strResult.length() > 0) {
std::size_t pos = strResult.find(":");
strResult = strResult.substr(pos + 2, 4);
if (strResult == "true") {
if (0 == i) *fma = true;
if (1 == i) *avx = true;
}
}
CloseHandle(hChildStdoutRd);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
printf("CreateProcess failed: %d\n", GetLastError());
*fma = false;
*avx = false;
return false;
}
}
if (bSuccess && (!(*fma) || !(*avx)))
return false;
}
return true;
}2.2 SDK のアクティベーション
2.3 モデルファイルの利用
2.4 素材ファイルの利用
Last updated