// IDirect3DDevice9 can not be used on WDDM OSes(>=vista) bool GLInteropResource::ensureWGL() { if (wgl) return true; static const char* ext[] = { "WGL_NV_DX_interop2", "WGL_NV_DX_interop", NULL, }; if (!OpenGLHelper::hasExtension(ext)) { // TODO: use wgl getprocaddress function (for qt4) qWarning("WGL_NV_DX_interop is required"); } wgl = new WGL(); memset(wgl, 0, sizeof(*wgl)); const QOpenGLContext *ctx = QOpenGLContext::currentContext(); //const for qt4 // QGLContext::getProcAddress(const QByteArray&), QOpenGLContext::getProcAddress(const QString&). So to work with QT_NO_CAST_FROM_ASCII we need a wrapper #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) #define QB(x) x #else #define QB(x) QString::fromLatin1(x) #endif wgl->DXSetResourceShareHandleNV = (PFNWGLDXSETRESOURCESHAREHANDLENVPROC)ctx->getProcAddress(QB("wglDXSetResourceShareHandleNV")); wgl->DXOpenDeviceNV = (PFNWGLDXOPENDEVICENVPROC)ctx->getProcAddress(QB("wglDXOpenDeviceNV")); wgl->DXCloseDeviceNV = (PFNWGLDXCLOSEDEVICENVPROC)ctx->getProcAddress(QB("wglDXCloseDeviceNV")); wgl->DXRegisterObjectNV = (PFNWGLDXREGISTEROBJECTNVPROC)ctx->getProcAddress(QB("wglDXRegisterObjectNV")); wgl->DXUnregisterObjectNV = (PFNWGLDXUNREGISTEROBJECTNVPROC)ctx->getProcAddress(QB("wglDXUnregisterObjectNV")); wgl->DXObjectAccessNV = (PFNWGLDXOBJECTACCESSNVPROC)ctx->getProcAddress(QB("wglDXObjectAccessNV")); wgl->DXLockObjectsNV = (PFNWGLDXLOCKOBJECTSNVPROC)ctx->getProcAddress(QB("wglDXLockObjectsNV")); wgl->DXUnlockObjectsNV = (PFNWGLDXUNLOCKOBJECTSNVPROC)ctx->getProcAddress(QB("wglDXUnlockObjectsNV")); #undef QB Q_ASSERT(wgl->DXRegisterObjectNV); return true; }
/* A callback that is invoked when the host sends data. * Param: * opaque - AdbClient instance. * connection - An opaque pointer that identifies connection with the ADB host. * buff, size - Buffer containing the host data. */ static void _adb_on_host_data(void* opaque, void* connection, const void* buff, int size) { AdbClient* const adb_client = (AdbClient*)opaque; D("ADB client %p(o=%p) received from the host %p %d bytes in %s", adb_client, adb_client->opaque, connection, size, QB(buff, size)); if (adb_client->state == ADBC_STATE_CONNECTED) { /* Dispatch data down to the guest. */ qemud_client_send(adb_client->qemud_client, (const uint8_t*)buff, size); } else { D("Unexpected data from ADB host %p while client %p(o=%p) is in state %d", connection, adb_client, adb_client->opaque, adb_client->state); } }
void adb_server_on_guest_message(void* opaque, const uint8_t* msg, int msglen) { AdbGuest* const adb_guest = (AdbGuest*)opaque; AdbHost* const adb_host = adb_guest->adb_host; if (adb_host != NULL) { D("Sending %d bytes to the ADB host: %s", msglen, QB(msg, msglen)); /* Lets see if we can send the data immediatelly... */ if (adb_host->pending_send_buffer == NULL) { /* There are no data that are pending to be sent to the host. Do the * direct send. */ const int sent = socket_send(adb_host->host_so, msg, msglen); if (sent < 0) { if (errno == EWOULDBLOCK) { } else { D("Unable to send data to ADB host: %s", strerror(errno)); } } else if (sent == 0) { /* Disconnect condition. */ _on_adb_host_disconnected(adb_host); } else if (sent < msglen) { /* Couldn't send everything. Schedule write via I/O callback. */ _adb_host_append_message(adb_host, msg + sent, msglen - sent); } } else { /* There are data that are pending to be sent to the host. We need * to append new data to the end of the pending data buffer. */ _adb_host_append_message(adb_host, msg, msglen); } } else { D("ADB host is disconneted and can't accept %d bytes in %s", msglen, QB(msg, msglen)); } }
/* Read I/O callback on ADB host socket. */ static void _on_adb_host_read(AdbHost* adb_host) { char buff[4096]; /* Read data from the socket. */ const int size = socket_recv(adb_host->host_so, buff, sizeof(buff)); if (size < 0) { D("Error while reading from ADB host %p(so=%d). Error: %s", adb_host, adb_host->host_so, strerror(errno)); } else if (size == 0) { /* This is a "disconnect" condition. */ _on_adb_host_disconnected(adb_host); } else { D("%s %d bytes received from ADB host %p(so=%d): %s", adb_host->adb_guest ? "Transfer" : "Pend", size, adb_host, adb_host->host_so, QB(buff, size)); /* Lets see if there is an ADB guest associated with this host, and it * is ready to receive host data. */ AdbGuest* const adb_guest = adb_host->adb_guest; if (adb_guest != NULL && adb_guest->is_connected) { /* Channel the data through... */ adb_guest->callbacks->on_read(adb_guest->opaque, adb_guest, buff, size); } else { /* Pend the data for the upcoming guest connection. */ if (adb_host->pending_data == NULL) { adb_host->pending_data = malloc(size); } else { adb_host->pending_data = realloc(adb_host->pending_data, adb_host->pending_data_size + size); } if (adb_host->pending_data != NULL) { memcpy(adb_host->pending_data + adb_host->pending_data_size, buff, size); adb_host->pending_data_size += size; } else { D("Unable to (re)allocate %d bytes for pending ADB host data", adb_host->pending_data_size + size); } } } }
/* A callback that is invoked when ADB guest sends data to the service. * Param: * opaque - AdbClient instance. * msg, msglen - Message received from the ADB guest. * client - adb QEMUD client. */ static void _adb_client_recv(void* opaque, uint8_t* msg, int msglen, QemudClient* client) { AdbClient* const adb_client = (AdbClient*)opaque; D("ADB client %p(o=%p) received from guest %d bytes in %s", adb_client, adb_client->opaque, msglen, QB(msg, msglen)); if (adb_client->state == ADBC_STATE_CONNECTED) { /* Connection is fully established. Dispatch the message to the host. */ adb_server_on_guest_message(adb_client->opaque, msg, msglen); return; } /* * At this point we expect either "accept", or "start" messages. Depending * on the state of the pipe (although small) these messages could be broken * into pieces. So, simply checking msg for "accept", or "start" may not * work. Lets collect them first in internal buffer, and then will see. */ /* Make sure tha message doesn't overflow the buffer. */ if ((msglen + adb_client->msg_cur) > sizeof(adb_client->msg_buffer)) { D("Unexpected message in ADB client."); adb_client->msg_cur = 0; return; } /* Append to current message. */ memcpy(adb_client->msg_buffer + adb_client->msg_cur, msg, msglen); adb_client->msg_cur += msglen; /* Properly dispatch the message, depending on the client state. */ switch (adb_client->state) { case ADBC_STATE_WAIT_ON_HOST: /* At this state the only message that is allowed is 'accept' */ if (adb_client->msg_cur == 6 && !memcmp(adb_client->msg_buffer, "accept", 6)) { adb_client->msg_cur = 0; /* Register ADB guest connection with the ADB server. */ adb_client->opaque = adb_server_register_guest(adb_client, &_adb_client_routines); if (adb_client->opaque == NULL) { D("Unable to register ADB guest with the ADB server."); /* KO the guest. */ qemud_client_send(adb_client->qemud_client, (const uint8_t*)"ko", 2); } } else { D("Unexpected guest request while waiting on ADB host to connect."); } break; case ADBC_STATE_HOST_CONNECTED: /* At this state the only message that is allowed is 'start' */ if (adb_client->msg_cur && !memcmp(adb_client->msg_buffer, "start", 5)) { adb_client->msg_cur = 0; adb_client->state = ADBC_STATE_CONNECTED; adb_server_complete_connection(adb_client->opaque); } else { D("Unexpected request while waiting on connection to start."); } break; default: D("Unexpected ADB guest request '%s' while client state is %d.", QB(msg, msglen), adb_client->state); break; } }
int QDWH ( DistMatrix<F>& A, typename Base<F>::type lowerBound, typename Base<F>::type upperBound ) { #ifndef RELEASE PushCallStack("QDWH"); #endif typedef typename Base<F>::type R; const Grid& g = A.Grid(); const int height = A.Height(); const int width = A.Width(); const R oneHalf = R(1)/R(2); const R oneThird = R(1)/R(3); if( height < width ) throw std::logic_error("Height cannot be less than width"); const R epsilon = lapack::MachineEpsilon<R>(); const R tol = 5*epsilon; const R cubeRootTol = Pow(tol,oneThird); // Form the first iterate Scale( 1/upperBound, A ); int numIts=0; R frobNormADiff; DistMatrix<F> ALast( g ); DistMatrix<F> Q( height+width, width, g ); DistMatrix<F> QT(g), QB(g); PartitionDown( Q, QT, QB, height ); DistMatrix<F> C( g ); DistMatrix<F> ATemp( g ); do { ++numIts; ALast = A; R L2; Complex<R> dd, sqd; if( Abs(1-lowerBound) < tol ) { L2 = 1; dd = 0; sqd = 1; } else { L2 = lowerBound*lowerBound; dd = Pow( 4*(1-L2)/(L2*L2), oneThird ); sqd = Sqrt( 1+dd ); } const Complex<R> arg = 8 - 4*dd + 8*(2-L2)/(L2*sqd); const R a = (sqd + Sqrt( arg )/2).real; const R b = (a-1)*(a-1)/4; const R c = a+b-1; const Complex<R> alpha = a-b/c; const Complex<R> beta = b/c; lowerBound = lowerBound*(a+b*L2)/(1+c*L2); if( c > 100 ) { // // The standard QR-based algorithm // QT = A; Scale( Sqrt(c), QT ); MakeIdentity( QB ); ExplicitQR( Q ); Gemm( NORMAL, ADJOINT, alpha/Sqrt(c), QT, QB, beta, A ); } else { // // Use faster Cholesky-based algorithm since A is well-conditioned // Identity( width, width, C ); Herk( LOWER, ADJOINT, F(c), A, F(1), C ); Cholesky( LOWER, C ); ATemp = A; Trsm( RIGHT, LOWER, ADJOINT, NON_UNIT, F(1), C, ATemp ); Trsm( RIGHT, LOWER, NORMAL, NON_UNIT, F(1), C, ATemp ); Scale( beta, A ); Axpy( alpha, ATemp, A ); } Axpy( F(-1), A, ALast ); frobNormADiff = Norm( ALast, FROBENIUS_NORM ); } while( frobNormADiff > cubeRootTol || Abs(1-lowerBound) > tol ); #ifndef RELEASE PopCallStack(); #endif return numIts; }
int Halley ( DistMatrix<F>& A, typename Base<F>::type upperBound ) { #ifndef RELEASE PushCallStack("Halley"); #endif typedef typename Base<F>::type R; const Grid& g = A.Grid(); const int height = A.Height(); const int width = A.Width(); const R oneHalf = R(1)/R(2); const R oneThird = R(1)/R(3); if( height < width ) throw std::logic_error("Height cannot be less than width"); const R epsilon = lapack::MachineEpsilon<R>(); const R tol = 5*epsilon; const R cubeRootTol = Pow(tol,oneThird); const R a = 3; const R b = 1; const R c = 3; // Form the first iterate Scale( 1/upperBound, A ); int numIts=0; R frobNormADiff; DistMatrix<F> ALast( g ); DistMatrix<F> Q( height+width, width, g ); DistMatrix<F> QT(g), QB(g); PartitionDown( Q, QT, QB, height ); DistMatrix<F> C( g ); DistMatrix<F> ATemp( g ); do { if( numIts > 100 ) throw std::runtime_error("Halley iteration did not converge"); ++numIts; ALast = A; // TODO: Come up with a test for when we can use the Cholesky approach if( true ) { // // The standard QR-based algorithm // QT = A; Scale( Sqrt(c), QT ); MakeIdentity( QB ); ExplicitQR( Q ); Gemm( NORMAL, ADJOINT, F(a-b/c)/Sqrt(c), QT, QB, F(b/c), A ); } else { // // Use faster Cholesky-based algorithm since A is well-conditioned // Identity( width, width, C ); Herk( LOWER, ADJOINT, F(c), A, F(1), C ); Cholesky( LOWER, C ); ATemp = A; Trsm( RIGHT, LOWER, ADJOINT, NON_UNIT, F(1), C, ATemp ); Trsm( RIGHT, LOWER, NORMAL, NON_UNIT, F(1), C, ATemp ); Scale( b/c, A ); Axpy( a-b/c, ATemp, A ); } Axpy( F(-1), A, ALast ); frobNormADiff = Norm( ALast, FROBENIUS_NORM ); } while( frobNormADiff > cubeRootTol ); #ifndef RELEASE PopCallStack(); #endif return numIts; }