void rawDisplayMatrixStream(const Matrix *inputMatrix) { const Ring *R = inputMatrix->get_ring(); const PolyRing *P = R->cast_to_PolyRing(); if (P == 0) { ERROR("expected a polynomial ring"); return; } int charac = P->charac(); int nvars = P->n_vars(); #if defined(HAVE_MATHICGB) mgb::GroebnerConfiguration configuration(charac, nvars); mgb::GroebnerInputIdealStream input(configuration); std::ostringstream computedStr; mgb::IdealStreamLog<> computed(computedStr, charac, nvars); mgb::IdealStreamChecker<decltype(computed)> checked(computed); matrixToStream(inputMatrix, checked); std::cout << "result: " << std::endl; std::cout << computedStr.str() << std::endl; #endif }
void MetaDataManager::launchComputing( Media *media ) { m_computeInProgress = true; m_mediaPlayer = new LibVLCpp::MediaPlayer; MetaDataWorker* worker = new MetaDataWorker( m_mediaPlayer, media ); connect( worker, SIGNAL( computed() ), this, SLOT( computingCompleted() ), Qt::DirectConnection ); connect( worker, SIGNAL( failed( Media* ) ), this, SLOT( computingFailed( Media* ) ), Qt::DirectConnection ); worker->compute(); }
// From http://tools.ietf.org/html/rfc6960#section-4.1.1: // "The hash shall be calculated over the value (excluding tag and length) of // the subject public key field in the issuer's certificate." // // From http://tools.ietf.org/html/rfc6960#appendix-B.1: // KeyHash ::= OCTET STRING -- SHA-1 hash of responder's public key // -- (i.e., the SHA-1 hash of the value of the // -- BIT STRING subjectPublicKey [excluding // -- the tag, length, and number of unused // -- bits] in the responder's certificate) static Result MatchKeyHash(TrustDomain& trustDomain, Input keyHash, const Input subjectPublicKeyInfo, /*out*/ bool& match) { if (keyHash.GetLength() != TrustDomain::DIGEST_LENGTH) { return Result::ERROR_OCSP_MALFORMED_RESPONSE; } static uint8_t hashBuf[TrustDomain::DIGEST_LENGTH]; Result rv = KeyHash(trustDomain, subjectPublicKeyInfo, hashBuf, sizeof hashBuf); if (rv != Success) { return rv; } Input computed(hashBuf); match = InputsAreEqual(computed, keyHash); return Success; }
plim_program compile_for_plim( const mig_graph& mig, const properties::ptr& settings, const properties::ptr& statistics ) { /* settings */ const auto verbose = get( settings, "verbose", false ); const auto progress = get( settings, "progress", false ); const auto enable_cost_function = get( settings, "enable_cost_function", true ); const auto generator_strategy = get( settings, "generator_strategy", 0u ); /* 0u: LIFO, 1u: FIFO */ /* timing */ properties_timer t( statistics ); plim_program program; const auto& info = mig_info( mig ); boost::dynamic_bitset<> computed( num_vertices( mig ) ); std::unordered_map<mig_function, memristor_index> func_to_rram; auto_index_generator<memristor_index> memristor_generator( generator_strategy == 0u ? auto_index_generator<memristor_index>::request_strategy::lifo : auto_index_generator<memristor_index>::request_strategy::fifo ); /* constant and all PIs are computed */ computed.set( info.constant ); for ( const auto& input : info.inputs ) { computed.set( input ); func_to_rram.insert( {{input, false}, memristor_generator.request()} ); } /* keep a priority queue for candidates invariant: candidates elements' children are all computed */ compilation_compare cmp( mig, enable_cost_function ); std::priority_queue<mig_node, std::vector<mig_node>, compilation_compare> candidates( cmp ); /* find initial candidates */ for ( const auto& node : boost::make_iterator_range( vertices( mig ) ) ) { /* PI and constant cannot be candidate */ if ( out_degree( node, mig ) == 0 ) { continue; } if ( all_children_computed( node, mig, computed ) ) { candidates.push( node ); } } const auto parent_edges = precompute_ingoing_edges( mig ); null_stream ns; std::ostream null_out( &ns ); boost::progress_display show_progress( num_vertices( mig ), progress ? std::cout : null_out ); /* synthesis loop */ while ( !candidates.empty() ) { ++show_progress; /* pick the best candidate */ auto candidate = candidates.top(); candidates.pop(); L( "[i] compute node " << candidate ); /* perform computation (e.g. mark which RRAM is used for this node) */ const auto children = get_children( mig, candidate ); boost::dynamic_bitset<> children_compl( 3u ); for ( const auto& f : index( children ) ) { children_compl.set( f.index, f.value.complemented ); } /* indexes and registers */ auto i_src_pos = 3u, i_src_neg = 3u, i_dst = 3u; plim_program::operand_t src_pos; plim_program::operand_t src_neg; memristor_index dst; /* find the inverter */ /* if there is one inverter */ if ( children_compl.count() == 1u ) { i_src_neg = children_compl.find_first(); if ( children[i_src_neg].node == 0u ) { src_neg = false; } else { src_neg = func_to_rram.at( {children[i_src_neg].node, false} ); } } /* if there are more than one inverters, but one of them is a constant */ else if ( children_compl.count() > 1u && children[children_compl.find_first()].node == 0u ) { i_src_neg = children_compl.find_next( children_compl.find_first() ); src_neg = func_to_rram.at( {children[i_src_neg].node, false} ); } /* if there is no inverter but a constant */ else if ( children_compl.count() == 0u && children[0u].node == 0u ) { i_src_neg = 0u; src_neg = !children[0u].complemented; } /* if there are more than one inverters */ else if ( children_compl.count() > 1u ) { do /* in order to escape early */ { /* pick an input that has multiple fanout */ for ( auto i = 0u; i < 3u; ++i ) { if ( !children_compl[i] ) continue; if ( cmp.fanout_count( children[i].node ) > 1u ) { i_src_neg = i; src_neg = func_to_rram.at( {children[i_src_neg].node, false} ); break; } } if ( i_src_neg < 3u ) { break; } i_src_neg = children_compl.find_first(); src_neg = func_to_rram.at( {children[i_src_neg].node, false} ); } while ( false ); } /* if there is no inverter */ else { do /* in order to escape early */ { /* pick an input that has multiple fanout */ for ( auto i = 0u; i < 3u; ++i ) { const auto it_reg = func_to_rram.find( {children[i].node, true} ); if ( it_reg != func_to_rram.end() ) { i_src_neg = i; src_neg = it_reg->second; break; } } if ( i_src_neg < 3u ) { break; } /* pick an input that has multiple fanout */ for ( auto i = 0u; i < 3u; ++i ) { if ( cmp.fanout_count( children[i].node ) > 1u ) { i_src_neg = i; break; } } /* or pick the first one */ if ( i_src_neg == 3u ) { i_src_neg = 0u; } /* create new register for inversion */ const auto inv_result = memristor_generator.request(); program.invert( inv_result, func_to_rram.at( {children[i_src_neg].node, false} ) ); func_to_rram.insert( {{children[i_src_neg].node, true}, inv_result} ); src_neg = inv_result; } while ( false ); } children_compl.reset( i_src_neg ); /* find the destination */ unsigned oa, ob; std::tie( oa, ob ) = three_without( i_src_neg ); /* if there is a child with one fan-out */ /* check whether they fulfill the requirements (non-constant and one fan-out) */ const auto oa_c = children[oa].node != 0u && cmp.fanout_count( children[oa].node ) == 1u; const auto ob_c = children[ob].node != 0u && cmp.fanout_count( children[ob].node ) == 1u; if ( oa_c || ob_c ) { /* first check for complemented cases (to avoid them for last operand) */ std::unordered_map<mig_function, memristor_index>::const_iterator it; if ( oa_c && children[oa].complemented && ( it = func_to_rram.find( {children[oa].node, true} ) ) != func_to_rram.end() ) { i_dst = oa; dst = it->second; } else if ( ob_c && children[ob].complemented && ( it = func_to_rram.find( {children[ob].node, true} ) ) != func_to_rram.end() ) { i_dst = ob; dst = it->second; } else if ( oa_c && !children[oa].complemented ) { i_dst = oa; dst = func_to_rram.at( {children[oa].node, false} ); } else if ( ob_c && !children[ob].complemented ) { i_dst = ob; dst = func_to_rram.at( {children[ob].node, false} ); } } /* no destination found yet? */ if ( i_dst == 3u ) { /* create new work RRAM */ dst = memristor_generator.request(); /* is there a constant (if, then it's the first one) */ if ( children[oa].node == 0u ) { i_dst = oa; program.read_constant( dst, children[oa].complemented ); } /* is there another inverter, then load it with that one? */ else if ( children_compl.count() > 0u ) { i_dst = children_compl.find_first(); program.invert( dst, func_to_rram.at( {children[i_dst].node, false} ) ); } /* otherwise, pick first one */ else { i_dst = oa; program.assign( dst, func_to_rram.at( {children[i_dst].node, false} ) ); } } /* positive operand */ i_src_pos = 3u - i_src_neg - i_dst; const auto node = children[i_src_pos].node; if ( node == 0u ) { src_pos = children[i_src_pos].complemented; } else if ( children[i_src_pos].complemented ) { const auto it_reg = func_to_rram.find( {node, true} ); if ( it_reg == func_to_rram.end() ) { /* create new register for inversion */ const auto inv_result = memristor_generator.request(); program.invert( inv_result, func_to_rram.at( {node, false} ) ); func_to_rram.insert( {{node, true}, inv_result} ); src_pos = inv_result; } else { src_pos = it_reg->second; } } else { src_pos = func_to_rram.at( {node, false} ); } program.compute( dst, src_pos, src_neg ); func_to_rram.insert( {{candidate, false}, dst} ); /* free free registers */ for ( const auto& c : children ) { if ( cmp.remove_fanout( c.node ) == 0u && c.node != 0u ) { const auto reg = func_to_rram.at( {c.node, false} ); if ( reg != dst ) { memristor_generator.release( reg ); } const auto it_reg = func_to_rram.find( {c.node, true} ); if ( it_reg != func_to_rram.end() && it_reg->second != dst ) { memristor_generator.release( it_reg->second ); } } } /* update computed and find new candidates */ computed.set( candidate ); const auto it = parent_edges.find( candidate ); if ( it != parent_edges.end() ) /* if it has parents */ { for ( const auto& e : it->second ) { const auto parent = boost::source( e, mig ); if ( !computed[parent] && all_children_computed( parent, mig, computed ) ) { candidates.push( parent ); } } } L( " - src_pos: " << i_src_pos << std::endl << " - src_neg: " << i_src_neg << std::endl << " - dst: " << i_dst << std::endl ); } set( statistics, "step_count", (int)program.step_count() ); set( statistics, "rram_count", (int)program.rram_count() ); std::vector<int> write_counts( program.write_counts().begin(), program.write_counts().end() ); set( statistics, "write_counts", write_counts ); return program; }
// CertID ::= SEQUENCE { // hashAlgorithm AlgorithmIdentifier, // issuerNameHash OCTET STRING, -- Hash of issuer's DN // issuerKeyHash OCTET STRING, -- Hash of issuer's public key // serialNumber CertificateSerialNumber } static inline Result CertID(Reader& input, const Context& context, /*out*/ bool& match) { match = false; DigestAlgorithm hashAlgorithm; Result rv = der::DigestAlgorithmIdentifier(input, hashAlgorithm); if (rv != Success) { if (rv == Result::ERROR_INVALID_ALGORITHM) { // Skip entries that are hashed with algorithms we don't support. input.SkipToEnd(); return Success; } return rv; } Input issuerNameHash; rv = der::ExpectTagAndGetValue(input, der::OCTET_STRING, issuerNameHash); if (rv != Success) { return rv; } Input issuerKeyHash; rv = der::ExpectTagAndGetValue(input, der::OCTET_STRING, issuerKeyHash); if (rv != Success) { return rv; } Input serialNumber; rv = der::CertificateSerialNumber(input, serialNumber); if (rv != Success) { return rv; } if (!InputsAreEqual(serialNumber, context.certID.serialNumber)) { // This does not reference the certificate we're interested in. // Consume the rest of the input and return successfully to // potentially continue processing other responses. input.SkipToEnd(); return Success; } // TODO: support SHA-2 hashes. if (hashAlgorithm != DigestAlgorithm::sha1) { // Again, not interested in this response. Consume input, return success. input.SkipToEnd(); return Success; } if (issuerNameHash.GetLength() != TrustDomain::DIGEST_LENGTH) { return Result::ERROR_OCSP_MALFORMED_RESPONSE; } // From http://tools.ietf.org/html/rfc6960#section-4.1.1: // "The hash shall be calculated over the DER encoding of the // issuer's name field in the certificate being checked." uint8_t hashBuf[TrustDomain::DIGEST_LENGTH]; rv = context.trustDomain.DigestBuf(context.certID.issuer, hashBuf, sizeof(hashBuf)); if (rv != Success) { return rv; } Input computed(hashBuf); if (!InputsAreEqual(computed, issuerNameHash)) { // Again, not interested in this response. Consume input, return success. input.SkipToEnd(); return Success; } return MatchKeyHash(context.trustDomain, issuerKeyHash, context.certID.issuerSubjectPublicKeyInfo, match); }
void ObscuranceMainThread::run() { Q_ASSERT(m_volume); m_stopped = false; vtkVolumeRayCastMapper *mapper = vtkVolumeRayCastMapper::SafeDownCast(m_volume->GetMapper()); vtkEncodedGradientEstimator *gradientEstimator = mapper->GetGradientEstimator(); /// \TODO fent això aquí crec que va més ràpid, però s'hauria de comprovar i provar també amb l'Update() gradientEstimator->GetEncodedNormals(); // Creem els threads /// \todo QThread::idealThreadCount() amb Qt >= 4.3 int numberOfThreads = vtkMultiThreader::GetGlobalDefaultNumberOfThreads(); QVector<ObscuranceThread*> threads(numberOfThreads); // Variables necessàries vtkImageData *image = mapper->GetInput(); unsigned short *data = reinterpret_cast<unsigned short*>(image->GetPointData()->GetScalars()->GetVoidPointer(0)); int dataSize = image->GetPointData()->GetScalars()->GetSize(); int dimensions[3]; image->GetDimensions(dimensions); vtkIdType vtkIncrements[3]; image->GetIncrements(vtkIncrements); int increments[3]; increments[0] = vtkIncrements[0]; increments[1] = vtkIncrements[1]; increments[2] = vtkIncrements[2]; m_obscurance = new Obscurance(dataSize, hasColor(), m_doublePrecision); for (int i = 0; i < numberOfThreads; i++) { ObscuranceThread * thread = new ObscuranceThread(i, numberOfThreads, m_transferFunction); thread->setGradientEstimator(gradientEstimator); thread->setData(data, dataSize, dimensions, increments); thread->setObscuranceParameters(m_maximumDistance, m_function, m_variant, m_obscurance); thread->setSaliency(m_saliency, m_fxSaliencyA, m_fxSaliencyB, m_fxSaliencyLow, m_fxSaliencyHigh); threads[i] = thread; } // Estructures de dades reaprofitables QVector<Vector3> lineStarts; const QVector<Vector3> directions = getDirections(); int nDirections = directions.size(); // Iterem per les direccions for (int i = 0; i < nDirections && !m_stopped; i++) { const Vector3 &direction = directions.at(i); DEBUG_LOG(QString("Direcció %1: %2").arg(i).arg(direction.toString())); // Direcció dominant (0 = x, 1 = y, 2 = z) int dominant; Vector3 absDirection(qAbs(direction.x), qAbs(direction.y), qAbs(direction.z)); if (absDirection.x >= absDirection.y) { if (absDirection.x >= absDirection.z) { dominant = 0; } else { dominant = 2; } } else { if (absDirection.y >= absDirection.z) { dominant = 1; } else { dominant = 2; } } // Vector per avançar Vector3 forward; switch (dominant) { case 0: forward = Vector3(direction.x, direction.y, direction.z); break; case 1: forward = Vector3(direction.y, direction.z, direction.x); break; case 2: forward = Vector3(direction.z, direction.x, direction.y); break; } // La direcció x passa a ser 1 o -1 forward /= qAbs(forward.x); DEBUG_LOG(QString("forward = ") + forward.toString()); // Dimensions i increments segons la direcció dominant int x = dominant, y = (dominant + 1) % 3, z = (dominant + 2) % 3; int dimX = dimensions[x], dimY = dimensions[y], dimZ = dimensions[z]; int incX = increments[x], incY = increments[y], incZ = increments[z]; int sX = 1, sY = 1, sZ = 1; qptrdiff startDelta = 0; if (forward.x < 0.0) { startDelta += incX * (dimX - 1); // incX = -incX; forward.x = -forward.x; sX = -1; } if (forward.y < 0.0) { startDelta += incY * (dimY - 1); // incY = -incY; forward.y = -forward.y; sY = -1; } if (forward.z < 0.0) { startDelta += incZ * (dimZ - 1); // incZ = -incZ; forward.z = -forward.z; sZ = -1; } DEBUG_LOG(QString("forward = ") + forward.toString()); // Ara els 3 components són positius // Llista dels vòxels que són començament de línia getLineStarts(lineStarts, dimX, dimY, dimZ, forward); // int incXYZ[3] = { incX, incY, incZ }; int xyz[3] = { x, y, z }; int sXYZ[3] = { sX, sY, sZ }; // Iniciem els threads for (int j = 0; j < numberOfThreads; j++) { ObscuranceThread * thread = threads[j]; thread->setPerDirectionParameters(direction, forward, xyz, sXYZ, lineStarts, startDelta); thread->start(); } // Esperem que acabin els threads for (int j = 0; j < numberOfThreads; j++) { threads[j]->wait(); } emit progress(100 * (i + 1) / nDirections); } // Destruïm els threads for (int j = 0; j < numberOfThreads; j++) { delete threads[j]; } // Si han cancel·lat el procés ja podem plegar if (m_stopped) { emit progress(0); delete m_obscurance; m_obscurance = 0; return; } m_obscurance->normalize(); emit computed(); }
const Matrix * rawMGB(const Matrix *inputMatrix, int reducer, int spairGroupSize, // a value of 0 means let the algorithm choose int nthreads, M2_string logging) { const Ring *R = inputMatrix->get_ring(); const PolyRing *P = R->cast_to_PolyRing(); if (P == 0) { ERROR("expected a polynomial ring"); return 0; } if (nthreads < 0) { ERROR("mgb: expected a non-negative number of threads"); return 0; } int charac = P->charac(); int nvars = P->n_vars(); #if defined(HAVE_MATHICGB) mgb::GroebnerConfiguration configuration(charac, nvars); const auto reducerType = reducer == 0 ? mgb::GroebnerConfiguration::ClassicReducer : mgb::GroebnerConfiguration::MatrixReducer; configuration.setReducer(reducerType); configuration.setMaxSPairGroupSize(spairGroupSize); configuration.setMaxThreadCount(nthreads); std::string log(logging->array, logging->len); configuration.setLogging(log.c_str()); std::vector<int> mat; bool base_is_revlex = true; // Now set the monomial ordering info if (!monomialOrderingToMatrix(* P->getMonoid()->getMonomialOrdering(), mat, base_is_revlex)) { ERROR("monomial ordering is not appropriate for Groebner basis computation"); return 0; } configuration.setMonomialOrder((base_is_revlex ? mgb::GroebnerConfiguration::BaseOrder::ReverseLexicographicBaseOrder : mgb::GroebnerConfiguration::BaseOrder::LexicographicBaseOrder), mat); #if 0 // Debug information printf("Setting monomial order:"); for (size_t i=0; i<mat.size(); i++) printf("%d ", mat[i]); printf("\n"); printf(" Base=%d\n", base_is_revlex); #endif mgb::GroebnerInputIdealStream input(configuration); std::ostringstream computedStr; mgb::IdealStreamLog<> computed(computedStr, charac, nvars); mgb::IdealStreamChecker<decltype(computed)> checkedOut(computed); matrixToStream(inputMatrix, input); MatrixStream matStream(P); // mgb::computeGroebnerBasis(input, checked); mgb::computeGroebnerBasis(input, matStream); const Matrix* result = matStream.value(); // rawDisplayMatrixStream(result); return result; #endif return inputMatrix; }