bool Pipe::readMessage(std::vector<BYTE> &Message) { MLIB_ASSERT_STR(m_handle != nullptr, "Pipe invalid in Pipe::ReadMessage"); DWORD BytesReady = 0; BOOL success = PeekNamedPipe( m_handle, nullptr, 0, nullptr, &BytesReady, nullptr); MLIB_ASSERT_STR(success != FALSE, "PeekNamedPipe failed in Pipe::ReadMessage"); Message.resize(BytesReady); if(BytesReady == 0) { return false; } DWORD BytesRead; success = ReadFile( m_handle, // handle to pipe &Message[0], // buffer to receive data (DWORD)Message.size(), // size of buffer &BytesRead, // number of bytes read nullptr); // not overlapped I/O MLIB_ASSERT_STR(success != FALSE && BytesRead > 0, "ReadFile failed in Pipe::ReadMessage"); return true; }
UINT Pipe::activeInstances() { MLIB_ASSERT_STR(m_handle != nullptr, "Pipe invalid in Pipe::ActiveInstances"); DWORD Instances; BOOL success = GetNamedPipeHandleState( m_handle, nullptr, &Instances, nullptr, nullptr, nullptr, 0); MLIB_ASSERT_STR(success != FALSE, "GetNamedPipeHandleState failed in Pipe::ActiveInstances"); return Instances; }
std::string Pipe::userName() { MLIB_ASSERT_STR(m_handle != nullptr, "Pipe invalid in Pipe::UserName"); char buffer[512]; BOOL success = GetNamedPipeHandleStateA( m_handle, nullptr, nullptr, nullptr, nullptr, buffer, 512); MLIB_ASSERT_STR(success != FALSE, "GetNamedPipeHandleState failed in Pipe::UserName"); return std::string(buffer); }
void Pipe::sendMessage(const BYTE *Message, UINT MessageLength) { if(Message == nullptr || MessageLength == 0) return; MLIB_ASSERT_STR(m_handle != nullptr, "Pipe invalid in Pipe::SendMessage"); DWORD BytesWritten; BOOL success = WriteFile( m_handle, // pipe handle Message, // message MessageLength, // message length &BytesWritten, // bytes written nullptr); // not overlapped MLIB_ASSERT_STR(success != FALSE, "WriteFile failed in Pipe::ReadMessage"); MLIB_ASSERT_STR(BytesWritten == MessageLength, "WriteFile failed to send entire message in Pipe::ReadMessage"); }
void Pipe::connectToPipe(const std::string &pipeName) { //Console::log("Connecting to " + pipeName); closePipe(); bool done = false; while(!done) { m_handle = CreateFileA( pipeName.c_str(), // pipe name GENERIC_READ | // read and write access GENERIC_WRITE, 0, // no sharing nullptr, // default security attributes OPEN_EXISTING, // opens existing pipe 0, // default attributes nullptr); // no template file if(m_handle != INVALID_HANDLE_VALUE) { done = true; } Sleep(100); } //cout << "Connected" << endl; //DWORD mode = PIPE_READMODE_MESSAGE; DWORD mode = PIPE_READMODE_BYTE; BOOL success = SetNamedPipeHandleState( m_handle, // pipe handle &mode, // new pipe mode nullptr, // don't set maximum bytes nullptr); // don't set maximum time MLIB_ASSERT_STR(success != FALSE, "SetNamedPipeHandleState failed in Pipe::ConnectToPipe"); }
FloatType DenseMatrix<FloatType>::maxMagnitude() const { MLIB_ASSERT_STR(valid(), "dense matrix has invalid entries"); double result = 0.0; for(UINT row = 0; row < m_rows; row++) for(UINT col = 0; col < m_cols; col++) result = std::max(result, fabs(m_dataPtr[row * m_cols + col])); return result; }
DenseMatrix<FloatType> DenseMatrix<FloatType>::subtract(const DenseMatrix<FloatType> &A, const DenseMatrix<FloatType> &B) { MLIB_ASSERT_STR(A.rows() == B.rows() && A.cols() == B.cols(), "invalid matrix dimensions"); const UINT rows = A.m_rows; const UINT cols = A.m_cols; DenseMatrix<FloatType> result(A.m_rows, A.m_cols); for(UINT row = 0; row < rows; row++) for(UINT col = 0; col < cols; col++) result.m_dataPtr[row * cols + col] = A.m_dataPtr[row * cols + col] - B.m_dataPtr[row * cols + col]; return result; }
bool Pipe::messagePresent() { MLIB_ASSERT_STR(m_handle != nullptr, "Pipe invalid in Pipe::MessagePresent"); DWORD BytesReady = 0; DWORD BytesLeft = 0; BOOL success = PeekNamedPipe( m_handle, nullptr, 0, nullptr, &BytesReady, &BytesLeft); //MLIB_ASSERT_STR(success != FALSE, "PeekNamedPipe failed in Pipe::MessagePresent"); return (BytesReady > 0); }
std::vector<FloatType> DenseMatrix<FloatType>::multiply(const DenseMatrix<FloatType> &A, const std::vector<FloatType> &B) { MLIB_ASSERT_STR(A.cols() == B.size(), "invalid dimensions"); const int rows = A.m_rows; const UINT cols = A.m_cols; std::vector<FloatType> result(rows); //#ifdef MLIB_OPENMP //#pragma omp parallel for //#endif for(int row = 0; row < rows; row++) { FloatType val = 0.0; for(UINT col = 0; col < cols; col++) val += A.m_dataPtr[row * cols + col] * B[col]; result[row] = val; } return result; }
void D3D11ShaderManager::registerShader( const std::string&filename, const std::string& shaderName, const std::string& entryPointVS, const std::string& shaderModelVS, const std::string& entryPointPS, const std::string& shaderModelPS, const std::vector<std::pair<std::string, std::string>>& shaderMacros ) { MLIB_ASSERT_STR(m_graphics != NULL, "shader manager not initialized"); // in case the shader exists return if (m_shaders.count(shaderName) == 0) { auto &shaders = m_shaders[shaderName]; shaders.vs.load(*m_graphics, filename, entryPointVS, shaderModelVS, shaderMacros); shaders.ps.load(*m_graphics, filename, entryPointPS, shaderModelPS, shaderMacros); } }
RGBColor::RGBColor(const std::string &hex) { MLIB_ASSERT_STR(hex.length() >= 6, "bad rgb hex code"); size_t offset = hex.length() - 6; const char* carray = hex.c_str(); char channel[3]; std::vector<BYTE> color(3); for (UINT c = 0; c < 3; c++) { channel[0] = carray[offset++]; channel[1] = carray[offset++]; channel[2] = '\0'; color[c] = (BYTE) strtol(channel, nullptr, 16); } r = color[0]; g = color[1]; b = color[2]; }
void ml::D3D11GeometryShader::init( GraphicsDevice& g, const std::string& filename, const std::string& entryPoint, const std::string& shaderModel, const std::vector<std::pair<std::string, std::string>>& shaderMacros) { m_graphics = &g.castD3D11(); releaseGPU(); SAFE_RELEASE(m_blob); m_filename = filename; //g.castD3D11().registerAsset(this); m_blob = D3D11Utility::CompileShader(m_filename, entryPoint, shaderModel, shaderMacros); MLIB_ASSERT_STR(m_blob != nullptr, "CompileShader failed"); createGPU(); }
DenseMatrix<FloatType> DenseMatrix<FloatType>::multiply(const DenseMatrix<FloatType> &A, const DenseMatrix<FloatType> &B) { MLIB_ASSERT_STR(A.cols() == B.rows(), "invalid dimensions"); const UINT rows = A.rows(); const UINT cols = B.cols(); const UINT innerCount = A.cols(); DenseMatrix<FloatType> result(rows, cols); for(UINT row = 0; row < rows; row++) for(UINT col = 0; col < cols; col++) { FloatType sum = 0.0; for(UINT inner = 0; inner < innerCount; inner++) sum += A(row, inner) * B(inner, col); result(row, col) = sum; } return result; }
ColorImageR8G8B8A8 LodePNG::load(const std::string &filename) { if (!ml::util::fileExists(filename)) { std::cout << ("LodePNG::load file not found: " + filename); return ColorImageR8G8B8A8(); } std::vector<BYTE> image; UINT width, height; UINT error = lodepng::decode(image, width, height, filename); MLIB_ASSERT_STR(!error, std::string(lodepng_error_text(error)) + ": " + filename); ColorImageR8G8B8A8 result; if (!error) { result.allocate(width, height); memcpy(result.getPointer(), &image[0], 4 * width * height); } return result; }
void Pipe::createPipe(const std::string &pipeName, bool block) { //Console::log() << "creating pipe " << pipeName << std::endl; closePipe(); const UINT PipeBufferSize = 100000; DWORD dwRes; PSID pEveryoneSID = nullptr, pAdminSID = nullptr; PACL pACL = nullptr; PSECURITY_DESCRIPTOR pSD = nullptr; EXPLICIT_ACCESS ea[1]; SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY; SECURITY_ATTRIBUTES attributes; HKEY hkSub = nullptr; // Create a well-known SID for the Everyone group. BOOL success = AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID); MLIB_ASSERT_STR(success != FALSE, "AllocateAndInitializeSid failed in Pipe::CreatePipe"); // Initialize an EXPLICIT_ACCESS structure for an ACE. // The ACE will allow Everyone read access to the key. ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS)); ea[0].grfAccessPermissions = FILE_ALL_ACCESS; ea[0].grfAccessMode = SET_ACCESS; ea[0].grfInheritance= NO_INHERITANCE; ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID; // Create a new ACL that contains the new ACEs. dwRes = SetEntriesInAcl(1, ea, nullptr, &pACL); MLIB_ASSERT_STR(dwRes == ERROR_SUCCESS, "SetEntriesInAcl failed in Pipe::CreatePipe"); // Initialize a security descriptor. pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); MLIB_ASSERT_STR(pSD != nullptr, "LocalAlloc failed in Pipe::CreatePipe"); success = InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION); MLIB_ASSERT_STR(success != FALSE, "InitializeSecurityDescriptor failed in Pipe::CreatePipe"); // Add the ACL to the security descriptor. success = SetSecurityDescriptorDacl(pSD, TRUE, // bDaclPresent flag pACL, FALSE); MLIB_ASSERT_STR(success != FALSE, "SetSecurityDescriptorDacl failed in Pipe::CreatePipe"); // Initialize a security attributes structure. attributes.nLength = sizeof(SECURITY_ATTRIBUTES); attributes.lpSecurityDescriptor = pSD; attributes.bInheritHandle = FALSE; std::string fullPipeName = std::string("\\\\.\\pipe\\") + pipeName; m_handle = CreateNamedPipeA( fullPipeName.c_str(), // pipe name PIPE_ACCESS_DUPLEX, // read/write access PIPE_TYPE_MESSAGE | // message type pipe PIPE_READMODE_MESSAGE | // message-read mode PIPE_WAIT, // blocking mode PIPE_UNLIMITED_INSTANCES, // max. instances PipeBufferSize, // output buffer size PipeBufferSize, // input buffer size NMPWAIT_USE_DEFAULT_WAIT, // client time-out &attributes); // default security attribute MLIB_ASSERT_STR(m_handle != INVALID_HANDLE_VALUE, "CreateNamedPipe failed in Pipe::CreatePipe"); // // Block until a connection comes in // if(block) { Console::log("Pipe created, waiting for connection"); BOOL Connected = (ConnectNamedPipe(m_handle, nullptr) != 0); MLIB_ASSERT_STR(Connected != FALSE, "ConnectNamedPipe failed in Pipe::CreatePipe"); Console::log("Connected"); } else { //cout << "Not blocking for connection to complete" << endl; } }
void EigenSolverVTK<FloatType>::eigenSystemInternal(const DenseMatrix<FloatType> &M, FloatType **eigenvectors, FloatType *eigenvalues) const { MLIB_ASSERT_STR(M.isSymmetric(), "can only handle symmetric matrices"); const unsigned int rows = M.rows(); MLIB_ASSERT_STR(M.square() && M.rows() >= 2, "invalid matrix dimensions in EigenSolverVTK<T>::eigenSystem"); int i, j, k, iq, ip, numPos, n = int(rows); FloatType tresh, theta, tau, t, sm, s, h, g, c, tmp; FloatType bspace[4], zspace[4]; FloatType *b = bspace; FloatType *z = zspace; // // Jacobi iteration destroys the matrix so create a temporary copy // DenseMatrix<FloatType> a = M; // // only allocate memory if the matrix is large // if (n > 4) { b = new FloatType[n]; z = new FloatType[n]; } // // initialize // for (ip = 0; ip<n; ip++) { for (iq = 0; iq<n; iq++) { eigenvectors[ip][iq] = 0.0; } eigenvectors[ip][ip] = 1.0; } for (ip = 0; ip<n; ip++) { b[ip] = a(ip, ip); eigenvalues[ip] = FloatType(a(ip, ip)); z[ip] = 0.0; } // begin rotation sequence for (i = 0; i<VTK_MAX_ROTATIONS; i++) { sm = 0.0; for (ip = 0; ip<n - 1; ip++) { for (iq = ip + 1; iq<n; iq++) { sm += fabs(a(ip, iq)); } } if (sm == 0.0) { break; } if (i < 3) // first 3 sweeps { tresh = (FloatType)0.2*sm / (n*n); } else { tresh = (FloatType)0.0; } for (ip = 0; ip<n - 1; ip++) { for (iq = ip + 1; iq<n; iq++) { g = FloatType(100.0*fabs(a(ip, iq))); // after 4 sweeps if (i > 3 && (fabs(eigenvalues[ip]) + g) == fabs(eigenvalues[ip]) && (fabs(eigenvalues[iq]) + g) == fabs(eigenvalues[iq])) { a(ip, iq) = 0.0; } else if (fabs(a(ip, iq)) > tresh) { h = eigenvalues[iq] - eigenvalues[ip]; if ((fabs(h) + g) == fabs(h)) { t = (a(ip, iq)) / h; } else { theta = (FloatType)0.5*h / (a(ip, iq)); t = (FloatType)1.0 / (fabs(theta) + sqrt((FloatType)1.0 + theta*theta)); if (theta < 0.0) { t = -t; } } c = (FloatType)1.0 / sqrt(1 + t*t); s = t*c; tau = s / ((FloatType)1.0 + c); h = t*a(ip, iq); z[ip] -= h; z[iq] += h; eigenvalues[ip] -= FloatType(h); eigenvalues[iq] += FloatType(h); a(ip, iq) = (FloatType)0.0; // ip already shifted left by 1 unit for (j = 0; j <= ip - 1; j++) { VTK_ROTATE(a, j, ip, j, iq); } // ip and iq already shifted left by 1 unit for (j = ip + 1; j <= iq - 1; j++) { VTK_ROTATE(a, ip, j, j, iq); } // iq already shifted left by 1 unit for (j = iq + 1; j<n; j++) { VTK_ROTATE(a, ip, j, iq, j); } for (j = 0; j<n; j++) { #pragma warning ( disable : 4244 ) VTK_ROTATE2(eigenvectors, j, ip, j, iq); #pragma warning ( default : 4244 ) } } } } for (ip = 0; ip<n; ip++) { b[ip] += z[ip]; eigenvalues[ip] = FloatType(b[ip]); z[ip] = 0.0; } } if (i >= VTK_MAX_ROTATIONS) { //return false; } // sort eigenfunctions these changes do not affect accuracy for (j = 0; j<n - 1; j++) // boundary incorrect { k = j; tmp = eigenvalues[k]; for (i = j + 1; i<n; i++) // boundary incorrect, shifted already { if (eigenvalues[i] >= tmp) // why exchage if same? { k = i; tmp = eigenvalues[k]; } } if (k != j) { eigenvalues[k] = eigenvalues[j]; eigenvalues[j] = FloatType(tmp); for (i = 0; i<n; i++) { tmp = eigenvectors[i][j]; eigenvectors[i][j] = eigenvectors[i][k]; eigenvectors[i][k] = FloatType(tmp); } } } // // insure eigenvector consistency (i.e., Jacobi can compute vectors that // are negative of one another (.707,.707,0) and (-.707,-.707,0). This can // reek havoc in hyperstreamline/other stuff. We will select the most // positive eigenvector. // int ceil_half_n = (n >> 1) + (n & 1); for (j = 0; j<n; j++) { for (numPos = 0, i = 0; i<n; i++) { if (eigenvectors[i][j] >= 0.0) { numPos++; } } // if ( numPos < ceil(double(n)/double(2.0)) ) if (numPos < ceil_half_n) { for (i = 0; i<n; i++) { eigenvectors[i][j] *= (FloatType)-1.0; } } } if (n > 4) { delete[] b; delete[] z; } }
void DenseMatrix<FloatType>::invertInPlace() { MLIB_ASSERT_STR(square(), "DenseMatrix<D>::invertInPlace called on non-square matrix"); for (UINT i = 1; i < m_rows; i++) { (*this)(0, i) /= (*this)(0, 0); } for (UINT i = 1; i < m_rows; i++) { // // do a column of L // for (UINT j = i; j < m_rows; j++) { FloatType sum = 0; for (UINT k = 0; k < i; k++) { sum += (*this)(j, k) * (*this)(k, i); } (*this)(j, i) -= sum; } if (i == m_rows - 1) { continue; } // // do a row of U // for (UINT j = i + 1; j < m_rows; j++) { FloatType sum = 0; for (UINT k = 0; k < i; k++) sum += (*this)(i, k) * (*this)(k, j); (*this)(i, j) = ((*this)(i, j) - sum) / (*this)(i, i); } } // // invert L // for (UINT i = 0; i < m_rows; i++) for (UINT j = i; j < m_rows; j++) { FloatType sum = (FloatType)1.0; if ( i != j ) { sum = 0; for (UINT k = i; k < j; k++ ) { sum -= (*this)(j, k) * (*this)(k, i); } } (*this)(j, i) = sum / (*this)(j, j); } // // invert U // for (UINT i = 0; i < m_rows; i++) for (UINT j = i; j < m_rows; j++) { if ( i == j ) { continue; } FloatType sum = 0; for (UINT k = i; k < j; k++) { FloatType val = (FloatType)1.0; if(i != k) { val = (*this)(i, k); } sum += (*this)(k, j) * val; } (*this)(i, j) = -sum; } // // final inversion // for (UINT i = 0; i < m_rows; i++) { for (UINT j = 0; j < m_rows; j++) { FloatType sum = 0; UINT larger = j; if(i > j) { larger = i; } for (UINT k = larger; k < m_rows; k++) { FloatType val = (FloatType)1.0; if(j != k) { val = (*this)(j, k); } sum += val * (*this)(k, i); } (*this)(j, i) = sum; } } //Assert(ElementsValid(), "Degenerate Matrix inversion."); }