TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( SPMD_apply_op, multivec_args_0_1_assign_zero_p0, Scalar )
{

  const RCP<const Teuchos::Comm<Ordinal> > comm =
    Teuchos::DefaultComm<Ordinal>::getComm();

  if (g_dumpRTOps) {
    RTOpPack::set_SPMD_apply_op_dump_out(rcpFromRef(out));
  }

  const int procRank = rank(*comm);

  const Ordinal localDim = (procRank == 0 ? 0 : g_localDim);
  PRINT_VAR(localDim);
  const Ordinal numCols = 3;
  PRINT_VAR(numCols);
  const Ordinal localOffset = computeLocalOffset(*comm, localDim);
  PRINT_VAR(localOffset);
  const Scalar val_init = -0.1;
  PRINT_VAR(val_init);

  RTOpPack::SubMultiVectorView<Scalar> mv =
    getLocalSubMultiVectorView<Scalar>(localOffset, localDim, numCols, val_init);

  const Scalar val = 1.2;
  PRINT_VAR(val);
  RTOpPack::TOpAssignScalar<Scalar> assignOp(val);
  RTOpPack::SPMD_apply_op<Scalar>(&*comm, assignOp, numCols, 0, 0, 1, &mv, 0);

  assertMultiVectorSums<Scalar>(*comm, mv, g_localDim, val, comm->getSize()-1,
    out, success);

  RTOpPack::set_SPMD_apply_op_dump_out(Teuchos::null);

}
Esempio n. 2
0
RSourceIndex::RSourceIndex(const std::string& context,
                           const std::string& code)
   : context_(context)
{
   // convert code to wide
   std::wstring wCode = string_utils::utf8ToWide(code, context);

   // determine where the linebreaks are and initialize an iterator
   // used for scanning them
   std::vector<std::size_t> newlineLocs;
   std::size_t nextNL = 0;
   while ( (nextNL = wCode.find(L'\n', nextNL)) != std::string::npos )
      newlineLocs.push_back(nextNL++);
   std::vector<std::size_t>::const_iterator newlineIter = newlineLocs.begin();
   std::vector<std::size_t>::const_iterator endNewlines = newlineLocs.end();

   // tokenize
   RTokens rTokens(wCode, RTokens::StripWhitespace | RTokens::StripComments);

   // scan for function, method, and class definitions (track indent level)
   int braceLevel = 0;
   std::wstring function(L"function");
   std::wstring set(L"set");
   std::wstring setGeneric(L"setGeneric");
   std::wstring setGroupGeneric(L"setGroupGeneric");
   std::wstring setMethod(L"setMethod");
   std::wstring setClass(L"setClass");
   std::wstring setClassUnion(L"setClassUnion");
   std::wstring setRefClass(L"setRefClass");
   std::wstring eqOp(L"=");
   std::wstring assignOp(L"<-");
   std::wstring parentAssignOp(L"<<-");
   for (std::size_t i=0; i<rTokens.size(); i++)
   {
      // initial name, qualifer, and type are nil
      RSourceItem::Type type = RSourceItem::None;
      std::wstring name;
      std::size_t tokenOffset = -1;
      bool isSetMethod = false;
      std::vector<RS4MethodParam> signature;

      // alias the token
      const RToken& token = rTokens.at(i);

      // see if this is a begin or end brace and update the level
      if (token.type() == RToken::LBRACE)
      {
         braceLevel++;
         continue;
      }

      else if (token.type() == RToken::RBRACE)
      {
         braceLevel--;
         continue;
      }
      // bail for non-identifiers
      else if (token.type() != RToken::ID)
      {
         continue;
      }

      // is this a potential method or class definition?
      if (token.contentStartsWith(set))
      {
         RSourceItem::Type setType = RSourceItem::None;

         if (token.contentEquals(setMethod))
         {
            isSetMethod = true;
            setType = RSourceItem::Method;
         }
         else if (token.contentEquals(setGeneric) ||
                  token.contentEquals(setGroupGeneric))
         {
            setType = RSourceItem::Method;
         }
         else if (token.contentEquals(setClass) ||
                  token.contentEquals(setClassUnion) ||
                  token.contentEquals(setRefClass))
         {
            setType = RSourceItem::Class;
         }
         else
         {
            continue;
         }

         // make sure there are at least 4 more tokens
         if ( (i + 3) >= rTokens.size())
            continue;

         // check for the rest of the token sequene for a valid call to set*
         if ( (rTokens.at(i+1).type() != RToken::LPAREN) ||
              (rTokens.at(i+2).type() != RToken::STRING) ||
              (rTokens.at(i+3).type() != RToken::COMMA))
            continue;

         // found a class or method definition (will find location below)
         type = setType;
         name = removeQuoteDelims(rTokens.at(i+2).content());
         tokenOffset = token.offset();

         // if this was a setMethod then try to lookahead for the signature
         if (isSetMethod)
         {
            parseSignature(rTokens.begin() + (i+4),
                           rTokens.end(),
                           &signature);
         }
      }

      // is this a function?
      else if (token.contentEquals(function))
      {
         // if there is no room for an operator and identifier prior
         // to the function then bail
         if (i < 2)
            continue;

         // check for an assignment operator
         const RToken& opToken = rTokens.at(i-1);
         if ( opToken.type() != RToken::OPER)
            continue;
         if (!opToken.isOperator(eqOp) &&
             !opToken.isOperator(assignOp) &&
             !opToken.isOperator(parentAssignOp))
            continue;

         // check for an identifier
         const RToken& idToken = rTokens.at(i-2);
         if ( idToken.type() != RToken::ID )
            continue;

         // if there is another previous token make sure it isn't a
         // comma or an open paren
         if ( i > 2 )
         {
            const RToken& prevToken = rTokens.at(i-3);
            if (prevToken.type() == RToken::LPAREN ||
                prevToken.type() == RToken::COMMA)
               continue;
         }

         // if we got this far then this is a function definition
         type = RSourceItem::Function;
         name = idToken.content();
         tokenOffset = idToken.offset();
      }
      else
      {
         continue;
      }

      // compute the line by starting at the current line index and
      // finding the first newline which is after the idToken offset
      newlineIter = std::upper_bound(newlineIter,
                                     endNewlines,
                                     tokenOffset);
      std::size_t line = newlineIter - newlineLocs.begin() + 1;

      // compute column by comparing the offset to the PREVIOUS newline
      // (guard against no previous newline)
      std::size_t column;
      if (line > 1)
         column = tokenOffset - *(newlineIter - 1);
      else
         column = tokenOffset;

      // add to index
      items_.push_back(RSourceItem(type,
                                   string_utils::wideToUtf8(name),
                                   signature,
                                   braceLevel,
                                   line,
                                   column));
   }
}