/*---------------------------------------------------------------------*/
int32_t
netmap_link_iface(void *ctxt, const unsigned char *iface,
		  const uint16_t batchsize, int8_t qid)
{
	TRACE_NETMAP_FUNC_START();

	char nifname[MAX_IFNAMELEN];
	netmap_module_context *nmc = (netmap_module_context *)ctxt;
	netmap_iface_context *nic = NULL;

	/* setting nm-ifname*/
	sprintf(nifname, "netmap:%s", iface);

	/* check if the interface has been registered with some other engine */
	netiface *nif = interface_find((char *)iface);
	if (nif == NULL) {
		nic = calloc(1, sizeof(netmap_iface_context));
		if (nic == NULL) {
			TRACE_ERR("Can't allocate memory for "
				  "netmap_iface_context (for %s)\n", iface);
			TRACE_NETMAP_FUNC_END();
			return -1;
		}
		/* resetting base_nmd */
		memset(&nic->base_nmd, 0, sizeof(struct nm_desc));

		/* resetting fd to -1 */
		nic->global_fd = nmc->local_fd = -1;

		/* use some extra rings */
		nic->base_nmd.req.nr_arg3 = NM_EXTRA_BUFS;
		nic->nmd_flags |= NM_OPEN_ARG3;

		nic->global_nmd = nm_open((char *)nifname, NULL, 
					  nic->nmd_flags, 
					  &nic->base_nmd);

		if (nic->global_nmd == NULL) {
			TRACE_LOG("Unable to open %s: %s\n", 
				  iface, strerror(errno));
			free(nic);
			TRACE_NETMAP_FUNC_END();
			return -1;
		}
		nic->global_fd = nic->global_nmd->fd;
		TRACE_DEBUG_LOG("mapped %dKB at %p\n", 
				nic->global_nmd->req.nr_memsize>>10, 
				nic->global_nmd->mem);
		TRACE_DEBUG_LOG("zerocopy %s", 
				(nic->global_nmd->mem == nic->base_nmd.mem) ? 
				"enabled\n" : "disabled\n");
		
		if (qid != -1) {
			nic->global_nmd->req.nr_flags = NR_REG_ONE_NIC;
			nic->global_nmd->req.nr_ringid = qid;
		}
	
		/* create interface entry */
		create_interface_entry(iface, (qid == -1) ? NO_QUEUES : HW_QUEUES, 
				       IO_NETMAP, nic, nmc->eng);
	} else { /* otherwise check if that interface can be registered */
Example #2
0
int
MPIDO_Allgather(const void *sendbuf,
                int sendcount,
                MPI_Datatype sendtype,
                void *recvbuf,
                int recvcount,
                MPI_Datatype recvtype,
                MPID_Comm * comm_ptr,
                int *mpierrno)
{
  /* *********************************
   * Check the nature of the buffers
   * *********************************
   */
/*  MPIDO_Coll_config config = {1,1,1,1,1,1};*/
   int config[6], i;
   MPID_Datatype * dt_null = NULL;
   MPI_Aint send_true_lb = 0;
   MPI_Aint recv_true_lb = 0;
   int rc, comm_size = comm_ptr->local_size;
   size_t send_size = 0;
   size_t recv_size = 0;
   volatile unsigned allred_active = 1;
   volatile unsigned allgather_active = 1;
   pami_xfer_t allred;
   for (i=0;i<6;i++) config[i] = 1;
   pami_metadata_t *my_md;


   allred.cb_done = allred_cb_done;
   allred.cookie = (void *)&allred_active;
   /* Pick an algorithm that is guaranteed to work for the pre-allreduce */
   /* TODO: This needs selection for fast(er|est) allreduce protocol */
   allred.algorithm = comm_ptr->mpid.coll_algorithm[PAMI_XFER_ALLREDUCE][0][0]; 
   allred.cmd.xfer_allreduce.sndbuf = (void *)config;
   allred.cmd.xfer_allreduce.stype = PAMI_TYPE_SIGNED_INT;
   allred.cmd.xfer_allreduce.rcvbuf = (void *)config;
   allred.cmd.xfer_allreduce.rtype = PAMI_TYPE_SIGNED_INT;
   allred.cmd.xfer_allreduce.stypecount = 6;
   allred.cmd.xfer_allreduce.rtypecount = 6;
   allred.cmd.xfer_allreduce.op = PAMI_DATA_BAND;

  char use_tree_reduce, use_alltoall, use_bcast, use_pami, use_opt;
  char *rbuf = NULL, *sbuf = NULL;

   use_alltoall = comm_ptr->mpid.allgathers[2];
   use_tree_reduce = comm_ptr->mpid.allgathers[0];
   use_bcast = comm_ptr->mpid.allgathers[1];
   use_pami = 
      (comm_ptr->mpid.user_selected_type[PAMI_XFER_ALLGATHER] == MPID_COLL_USE_MPICH) ? 0 : 1;
/*   if(sendbuf == MPI_IN_PLACE) use_pami = 0;*/
   use_opt = use_alltoall || use_tree_reduce || use_bcast || use_pami;


   TRACE_ERR("flags before: b: %d a: %d t: %d p: %d\n", use_bcast, use_alltoall, use_tree_reduce, use_pami);
   if(!use_opt)
   {
      if(unlikely(MPIDI_Process.verbose >= MPIDI_VERBOSE_DETAILS_ALL && comm_ptr->rank == 0))
         fprintf(stderr,"Using MPICH allgather algorithm\n");
      TRACE_ERR("No options set/available; using MPICH for allgather\n");
      MPIDI_Update_last_algorithm(comm_ptr, "ALLGATHER_MPICH");
      return MPIR_Allgather(sendbuf, sendcount, sendtype,
                            recvbuf, recvcount, recvtype,
                            comm_ptr, mpierrno);
   }
   if ((sendcount < 1 && sendbuf != MPI_IN_PLACE) || recvcount < 1)
      return MPI_SUCCESS;

   /* Gather datatype information */
   MPIDI_Datatype_get_info(recvcount,
			  recvtype,
        config[MPID_RECV_CONTIG],
			  recv_size,
			  dt_null,
			  recv_true_lb);

   send_size = recv_size;
   rbuf = (char *)recvbuf+recv_true_lb;

   if(sendbuf != MPI_IN_PLACE)
   {
      if(unlikely(MPIDI_Process.verbose >= MPIDI_VERBOSE_DETAILS_ALL))
         fprintf(stderr,"allgather MPI_IN_PLACE buffering\n");
      MPIDI_Datatype_get_info(sendcount,
                            sendtype,
                            config[MPID_SEND_CONTIG],
                            send_size,
                            dt_null,
                            send_true_lb);
      sbuf = (char *)sendbuf+send_true_lb;
   }
   else
   {
      sbuf = (char *)recvbuf+recv_size*comm_ptr->rank;
   }
/*   fprintf(stderr,"sendount: %d, recvcount: %d send_size: %zd
     recv_size: %zd\n", sendcount, recvcount, send_size,
     recv_size);*/

  /* verify everyone's datatype contiguity */
  /* Check buffer alignment now, since we're pre-allreducing anyway */
  /* Only do this if one of the glue protocols is likely to be used */
  if(use_alltoall || use_tree_reduce || use_bcast)
  {
     config[MPID_ALIGNEDBUFFER] =
               !((long)sendbuf & 0x0F) && !((long)recvbuf & 0x0F);

      /* #warning need to determine best allreduce for short messages */
      if(comm_ptr->mpid.preallreduces[MPID_ALLGATHER_PREALLREDUCE])
      {
         TRACE_ERR("Preallreducing in allgather\n");
         MPIDI_Post_coll_t allred_post;
         MPIDI_Context_post(MPIDI_Context[0], &allred_post.state,
                            MPIDI_Pami_post_wrapper, (void *)&allred);

         MPID_PROGRESS_WAIT_WHILE(allred_active);
     }


       use_alltoall = comm_ptr->mpid.allgathers[2] &&
            config[MPID_RECV_CONTIG] && config[MPID_SEND_CONTIG];;

      /* Note: some of the glue protocols use recv_size*comm_size rather than 
       * recv_size so we use that for comparison here, plus we pass that in
       * to those protocols. */
       use_tree_reduce = comm_ptr->mpid.allgathers[0] &&
         config[MPID_RECV_CONTIG] && config[MPID_SEND_CONTIG] &&
         config[MPID_RECV_CONTINUOUS] && (recv_size*comm_size % sizeof(int) == 0);

       use_bcast = comm_ptr->mpid.allgathers[1];

       TRACE_ERR("flags after: b: %d a: %d t: %d p: %d\n", use_bcast, use_alltoall, use_tree_reduce, use_pami);
   }
   if(use_pami)
   {
      TRACE_ERR("Using PAMI-level allgather protocol\n");
      pami_xfer_t allgather;
      allgather.cb_done = allgather_cb_done;
      allgather.cookie = (void *)&allgather_active;
      allgather.cmd.xfer_allgather.rcvbuf = rbuf;
      allgather.cmd.xfer_allgather.sndbuf = sbuf;
      allgather.cmd.xfer_allgather.stype = PAMI_TYPE_BYTE;
      allgather.cmd.xfer_allgather.rtype = PAMI_TYPE_BYTE;
      allgather.cmd.xfer_allgather.stypecount = send_size;
      allgather.cmd.xfer_allgather.rtypecount = recv_size;
      if(comm_ptr->mpid.user_selected_type[PAMI_XFER_ALLGATHER] == MPID_COLL_OPTIMIZED)
      {
         allgather.algorithm = comm_ptr->mpid.opt_protocol[PAMI_XFER_ALLGATHER][0];
         my_md = &comm_ptr->mpid.opt_protocol_md[PAMI_XFER_ALLGATHER][0];
      }
      else
      {
         allgather.algorithm = comm_ptr->mpid.user_selected[PAMI_XFER_ALLGATHER];
         my_md = &comm_ptr->mpid.user_metadata[PAMI_XFER_ALLGATHER];
      }

      if(unlikely( comm_ptr->mpid.user_selected_type[PAMI_XFER_ALLGATHER] == MPID_COLL_ALWAYS_QUERY ||
                   comm_ptr->mpid.user_selected_type[PAMI_XFER_ALLGATHER] == MPID_COLL_CHECK_FN_REQUIRED))
      {
         metadata_result_t result = {0};
         TRACE_ERR("Querying allgather protocol %s, type was: %d\n",
            my_md->name,
            comm_ptr->mpid.user_selected_type[PAMI_XFER_ALLGATHER]);
         result = my_md->check_fn(&allgather);
         TRACE_ERR("bitmask: %#X\n", result.bitmask);
         if(!result.bitmask)
         {
            fprintf(stderr,"Query failed for %s.\n",
               my_md->name);
         }
      }

      if(unlikely(MPIDI_Process.verbose >= MPIDI_VERBOSE_DETAILS_ALL && comm_ptr->rank == 0))
      {
         unsigned long long int threadID;
         MPIU_Thread_id_t tid;
         MPIU_Thread_self(&tid);
         threadID = (unsigned long long int)tid;
         fprintf(stderr,"<%llx> Using protocol %s for allgather on %u\n", 
                 threadID,
                 my_md->name,
              (unsigned) comm_ptr->context_id);
      }
      TRACE_ERR("Calling PAMI_Collective with allgather structure\n");
      MPIDI_Post_coll_t allgather_post;
      MPIDI_Context_post(MPIDI_Context[0], &allgather_post.state, MPIDI_Pami_post_wrapper, (void *)&allgather);
      TRACE_ERR("Allgather %s\n", MPIDI_Process.context_post.active>0?"posted":"invoked");

      MPIDI_Update_last_algorithm(comm_ptr, my_md->name);

      MPID_PROGRESS_WAIT_WHILE(allgather_active);
      TRACE_ERR("Allgather done\n");
      return PAMI_SUCCESS;
   }

   if(use_tree_reduce)
   {
      TRACE_ERR("Using allgather via allreduce\n");
      MPIDI_Update_last_algorithm(comm_ptr, "ALLGATHER_OPT_ALLREDUCE");
     rc = MPIDO_Allgather_allreduce(sendbuf, sendcount, sendtype,
                               recvbuf, recvcount, recvtype,
                               send_true_lb, recv_true_lb, send_size, recv_size*comm_size, comm_ptr, mpierrno);
      return rc;
   }
   if(use_alltoall)
   {
      TRACE_ERR("Using allgather via alltoall\n");
      MPIDI_Update_last_algorithm(comm_ptr, "ALLGATHER_OPT_ALLTOALL");
     rc = MPIDO_Allgather_alltoall(sendbuf, sendcount, sendtype,
                               recvbuf, recvcount, recvtype,
                               send_true_lb, recv_true_lb, send_size, recv_size*comm_size, comm_ptr, mpierrno);
      return rc;
   }

   if(use_bcast)
   {
      TRACE_ERR("Using allgather via bcast\n");
      MPIDI_Update_last_algorithm(comm_ptr, "ALLGATHER_OPT_BCAST");
     rc = MPIDO_Allgather_bcast(sendbuf, sendcount, sendtype,
                               recvbuf, recvcount, recvtype,
                               send_true_lb, recv_true_lb, send_size, recv_size*comm_size, comm_ptr, mpierrno);
      return rc;
   }
   
   /* Nothing used yet; dump to MPICH */
   if(unlikely(MPIDI_Process.verbose >= MPIDI_VERBOSE_DETAILS_ALL && comm_ptr->rank == 0))
      fprintf(stderr,"Using MPICH allgather algorithm\n");
   TRACE_ERR("Using allgather via mpich\n");
   MPIDI_Update_last_algorithm(comm_ptr, "ALLGATHER_MPICH");
   return MPIR_Allgather(sendbuf, sendcount, sendtype,
                         recvbuf, recvcount, recvtype,
                         comm_ptr, mpierrno);
}
Example #3
0
/*---------------------------------------------------------------------*/
void
start_listening_reqs()
{
	TRACE_BACKEND_FUNC_START();
	/* socket info about the listen sock */
	struct sockaddr_in serv; 
	int listen_fd, client_sock;
	struct epoll_event ev, events[EPOLL_MAX_EVENTS];
	int epoll_fd, nfds, n;	

	/* zero the struct before filling the fields */
	memset(&serv, 0, sizeof(serv));
	/* set the type of connection to TCP/IP */           
	serv.sin_family = AF_INET;
	/* set the address to any interface */                
	serv.sin_addr.s_addr = htonl(INADDR_ANY); 
	/* set the server port number */    
	serv.sin_port = htons(BRICKS_LISTEN_PORT);

	/* create bricks socket for listening remote shell requests */
	listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (listen_fd == -1) {
		TRACE_ERR("Failed to create listening socket for bricks\n");
		TRACE_BACKEND_FUNC_END();
	}
	
	/* bind serv information to mysocket */
	if (bind(listen_fd, (struct sockaddr *)&serv, sizeof(struct sockaddr)) == -1) {
		TRACE_ERR("Failed to bind listening socket to port %d for bricks\n",
			  BRICKS_LISTEN_PORT);
		TRACE_BACKEND_FUNC_END();
	}
	
	/* start listening, allowing a queue of up to 1 pending connection */
	if (listen(listen_fd, LISTEN_BACKLOG) == -1) {
		TRACE_ERR("Failed to start listen on port %d (for bricks)\n",
			  BRICKS_LISTEN_PORT);
		TRACE_BACKEND_FUNC_END();
	}

	/* set up the epolling structure */
	epoll_fd = epoll_create(EPOLL_MAX_EVENTS);
	if (epoll_fd == -1) {
		TRACE_ERR("BRICKS failed to create an epoll fd!\n");
		TRACE_BACKEND_FUNC_END();
		return;
	}


	/* register listening socket */
	ev.events = EPOLLIN | EPOLLOUT;
	ev.data.fd = listen_fd;
	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev) == -1) {
		TRACE_LOG("BRICKS failed to exe epoll_ctl for fd: %d\n",
			  epoll_fd);
		TRACE_BACKEND_FUNC_END();
		return;
	}
	
	/*
	 * Main loop that processes incoming remote shell commands
	 * if the remote request arrives at listen_fd, it accepts connections
	 * and creates a new 'shell'
	 * if the remote request comes from a client request, it calls
	 * the lua string command function to execute the command
	 */

	do {
		/* wait for new epoll requests */
		nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, -1);
		if (nfds == -1) {
			if (errno == EINTR) continue;
			TRACE_ERR("BRICKS poll error: %s\n", strerror(errno));
			TRACE_BACKEND_FUNC_END();
		}
		/* got some request... now process each one of them */
		for (n = 0; n < nfds; n++) {
			/* the request is for a new shell */
			if (events[n].data.fd == listen_fd) {
				client_sock = accept(listen_fd, NULL, NULL);
				if (client_sock < 0) {
					TRACE_ERR("accept failed: %s\n", strerror(errno));
					TRACE_BACKEND_FUNC_END();
				}
				lua_kickoff(LUA_EXE_STR, &client_sock);
				/* add client_sock and listen_fd for epolling again */
				ev.data.fd = client_sock;
				ev.events = EPOLLIN;
				if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1) {
					TRACE_ERR("Can't register client sock for epolling!\n");
					TRACE_BACKEND_FUNC_END();
				}
				ev.data.fd = listen_fd;
				ev.events = EPOLLIN | EPOLLOUT;
				if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, ev.data.fd, &ev) == -1) {
					TRACE_ERR("Can't register client sock for epolling!\n");
					TRACE_BACKEND_FUNC_END();
				}
			} else { /* events[n].data.fd == regular sock */
				/* 
				 * if the client socket has incoming read, this means we are getting
				 * some lua commands.... execute them 
				 */
				if (events[n].events == EPOLLIN || events[n].events == EPOLLPRI) {
					lua_kickoff(LUA_EXE_STR, &events[n].data.fd);
					ev.data.fd = events[n].data.fd;
					ev.events = EPOLLIN;
					if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, ev.data.fd, &ev) == -1) {
						TRACE_ERR("Can't register client sock for epolling!\n");
						TRACE_BACKEND_FUNC_END();
					}
				} else { /* the only other epoll request is for error output.. close the socket then */
					ev.data.fd = events[n].data.fd;
					if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, ev.data.fd, &ev) == -1) {
						TRACE_ERR("Can't register client sock for epolling!\n");
						TRACE_BACKEND_FUNC_END();
					}	
					close(ev.data.fd);
				}
			}
		}
	} while (1);

	TRACE_BACKEND_FUNC_END();
}
Example #4
0
bool RuleTrieLoader::Load(const std::vector<FactorType> &input,
                          const std::vector<FactorType> &output,
                          const std::string &inFile,
                          const RuleTableFF &ff,
                          RuleTrie &trie)
{
  PrintUserTime(std::string("Start loading text phrase table. Moses format"));

  const StaticData &staticData = StaticData::Instance();
  const std::string &factorDelimiter = staticData.GetFactorDelimiter();

  std::size_t count = 0;

  std::ostream *progress = NULL;
  IFVERBOSE(1) progress = &std::cerr;
  util::FilePiece in(inFile.c_str(), progress);

  // reused variables
  std::vector<float> scoreVector;
  StringPiece line;

  double_conversion::StringToDoubleConverter converter(double_conversion::StringToDoubleConverter::NO_FLAGS, NAN, NAN, "inf", "nan");

  while(true) {
    try {
      line = in.ReadLine();
    } catch (const util::EndOfFileException &e) {
      break;
    }

    util::TokenIter<util::MultiCharacter> pipes(line, "|||");
    StringPiece sourcePhraseString(*pipes);
    StringPiece targetPhraseString(*++pipes);
    StringPiece scoreString(*++pipes);

    StringPiece alignString;
    if (++pipes) {
      StringPiece temp(*pipes);
      alignString = temp;
    }

    if (++pipes) {
      StringPiece str(*pipes); //counts
    }

    bool isLHSEmpty = (sourcePhraseString.find_first_not_of(" \t", 0) == std::string::npos);
    if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
      TRACE_ERR( ff.GetFilePath() << ":" << count << ": pt entry contains empty target, skipping\n");
      continue;
    }

    scoreVector.clear();
    for (util::TokenIter<util::AnyCharacter, true> s(scoreString, " \t"); s; ++s) {
      int processed;
      float score = converter.StringToFloat(s->data(), s->length(), &processed);
      UTIL_THROW_IF2(std::isnan(score), "Bad score " << *s << " on line " << count);
      scoreVector.push_back(FloorScore(TransformScore(score)));
    }
    const size_t numScoreComponents = ff.GetNumScoreComponents();
    if (scoreVector.size() != numScoreComponents) {
      UTIL_THROW2("Size of scoreVector != number (" << scoreVector.size() << "!="
    		  	  << numScoreComponents << ") of score components on line " << count);
    }

    // parse source & find pt node

    // constituent labels
    Word *sourceLHS = NULL;
    Word *targetLHS;

    // create target phrase obj
    TargetPhrase *targetPhrase = new TargetPhrase(&ff);
    // targetPhrase->CreateFromString(Output, output, targetPhraseString, factorDelimiter, &targetLHS);
    targetPhrase->CreateFromString(Output, output, targetPhraseString, &targetLHS);
    // source
    Phrase sourcePhrase;
    // sourcePhrase.CreateFromString(Input, input, sourcePhraseString, factorDelimiter, &sourceLHS);
    sourcePhrase.CreateFromString(Input, input, sourcePhraseString, &sourceLHS);

    // rest of target phrase
    targetPhrase->SetAlignmentInfo(alignString);
    targetPhrase->SetTargetLHS(targetLHS);

    //targetPhrase->SetDebugOutput(string("New Format pt ") + line);

    if (++pipes) {
      StringPiece sparseString(*pipes);
      targetPhrase->SetSparseScore(&ff, sparseString);
    }

    if (++pipes) {
      StringPiece propertiesString(*pipes);
      targetPhrase->SetProperties(propertiesString);
    }

    targetPhrase->GetScoreBreakdown().Assign(&ff, scoreVector);
    targetPhrase->EvaluateInIsolation(sourcePhrase, ff.GetFeaturesToApply());

    TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection(
        trie, sourcePhrase, *targetPhrase, sourceLHS);
    phraseColl.Add(targetPhrase);

    // not implemented correctly in memory pt. just delete it for now
    delete sourceLHS;

    count++;
  }

  // sort and prune each target phrase collection
  if (ff.GetTableLimit()) {
    SortAndPrune(trie, ff.GetTableLimit());
  }

  return true;
}
bool LexicalReorderingTableTree::Create(std::istream& inFile,
                                        const std::string& outFileName)
{
  std::string line;
  //TRACE_ERR("Entering Create...\n");
  std::string
  ofn(outFileName+".binlexr.srctree"),
      oft(outFileName+".binlexr.tgtdata"),
      ofi(outFileName+".binlexr.idx"),
      ofsv(outFileName+".binlexr.voc0"),
      oftv(outFileName+".binlexr.voc1");


  FILE *os = fOpen(ofn.c_str(),"wb");
  FILE *ot = fOpen(oft.c_str(),"wb");

  //TRACE_ERR("opend files....\n");

  typedef PrefixTreeSA<LabelId,OFF_T> PSA;
  PSA *psa = new PSA;
  PSA::setDefault(InvalidOffT);
  WordVoc* voc[3];

  LabelId currFirstWord = InvalidLabelId;
  IPhrase currKey;

  Candidates         cands;
  std::vector<OFF_T> vo;
  size_t lnc = 0;
  size_t numTokens    = 0;
  size_t numKeyTokens = 0;
  while(getline(inFile, line)) {
    ++lnc;
    if(0 == lnc % 10000) {
      TRACE_ERR(".");
    }
    IPhrase key;
    Scores   score;

    std::vector<std::string> tokens = TokenizeMultiCharSeparator(line, "|||");
    std::string w;
    if(1 == lnc) {
      //do some init stuff in the first line
      numTokens = tokens.size();
      if(tokens.size() == 2) { //f ||| score
        numKeyTokens = 1;
        voc[0] = new WordVoc();
        voc[1] = 0;
      } else if(3 == tokens.size() || 4 == tokens.size()) { //either f ||| e ||| score or f ||| e ||| c ||| score
        numKeyTokens = 2;
        voc[0] = new WordVoc(); //f voc
        voc[1] = new WordVoc(); //e voc
        voc[2] = voc[1];        //c & e share voc
      }
    } else {
      //sanity check ALL lines must have same number of tokens
      CHECK(numTokens == tokens.size());
    }
    size_t phrase = 0;
    for(; phrase < numKeyTokens; ++phrase) {
      //conditioned on more than just f... need |||
      if(phrase >=1) {
        key.push_back(PrefixTreeMap::MagicWord);
      }
      std::istringstream is(tokens[phrase]);
      while(is >> w) {
        key.push_back(voc[phrase]->add(w));
      }
    }
    //collect all non key phrases, i.e. c
    std::vector<IPhrase> tgt_phrases;
    tgt_phrases.resize(numTokens - numKeyTokens - 1);
    for(size_t j = 0; j < tgt_phrases.size(); ++j, ++phrase) {
      std::istringstream is(tokens[numKeyTokens + j]);
      while(is >> w) {
        tgt_phrases[j].push_back(voc[phrase]->add(w));
      }
    }
    //last token is score
    std::istringstream is(tokens[numTokens-1]);
    while(is >> w) {
      score.push_back(atof(w.c_str()));
    }
    //transform score now...
    std::transform(score.begin(),score.end(),score.begin(),TransformScore);
    std::transform(score.begin(),score.end(),score.begin(),FloorScore);
    std::vector<Scores> scores;
    scores.push_back(score);

    if(key.empty()) {
      TRACE_ERR("WARNING: empty source phrase in line '"<<line<<"'\n");
      continue;
    }
    //first time inits
    if(currFirstWord == InvalidLabelId) {
      currFirstWord = key[0];
    }
    if(currKey.empty()) {
      currKey = key;
      //insert key into tree
      CHECK(psa);
      PSA::Data& d = psa->insert(key);
      if(d == InvalidOffT) {
        d = fTell(ot);
      } else {
        TRACE_ERR("ERROR: source phrase already inserted (A)!\nline(" << lnc << "): '" << line << "\n");
        return false;
      }
    }
    if(currKey != key) {
      //ok new key
      currKey = key;
      //a) write cands for old key
      cands.writeBin(ot);
      cands.clear();
      //b) check if we need to move on to new tree root
      if(key[0] != currFirstWord) {
        // write key prefix tree to file and clear
        PTF pf;
        if(currFirstWord >= vo.size()) {
          vo.resize(currFirstWord+1,InvalidOffT);
        }
        vo[currFirstWord] = fTell(os);
        pf.create(*psa, os);
        // clear
        delete psa;
        psa = new PSA;
        currFirstWord = key[0];
      }
      //c) insert key into tree
      CHECK(psa);
      PSA::Data& d = psa->insert(key);
      if(d == InvalidOffT) {
        d = fTell(ot);
      } else {
        TRACE_ERR("ERROR: source phrase already inserted (A)!\nline(" << lnc << "): '" << line << "\n");
        return false;
      }
    }
    cands.push_back(GenericCandidate(tgt_phrases, scores));
  }
  if (lnc == 0) {
    TRACE_ERR("ERROR: empty lexicalised reordering file\n" << std::endl);
    return false;
  }
  //flush remainders
  cands.writeBin(ot);
  cands.clear();
  //process last currFirstWord
  PTF pf;
  if(currFirstWord >= vo.size()) {
    vo.resize(currFirstWord+1,InvalidOffT);
  }
  vo[currFirstWord] = fTell(os);
  pf.create(*psa,os);
  delete psa;
  psa=0;

  fClose(os);
  fClose(ot);
  /*
  std::vector<size_t> inv;
  for(size_t i = 0; i < vo.size(); ++i){
    if(vo[i] == InvalidOffT){
      inv.push_back(i);
    }
  }
  if(inv.size()) {
    TRACE_ERR("WARNING: there are src voc entries with no phrase "
        "translation: count "<<inv.size()<<"\n"
        "There exists phrase translations for "<<vo.size()-inv.size()
        <<" entries\n");
  }
  */
  FILE *oi = fOpen(ofi.c_str(),"wb");
  fWriteVector(oi,vo);
  fClose(oi);

  if(voc[0]) {
    voc[0]->Write(ofsv);
    delete voc[0];
  }
  if(voc[1]) {
    voc[1]->Write(oftv);
    delete voc[1];
  }
  return true;
}
Example #6
0
int MPIDO_Reduce(const void *sendbuf, 
                 void *recvbuf, 
                 int count, 
                 MPI_Datatype datatype,
                 MPI_Op op, 
                 int root, 
                 MPID_Comm *comm_ptr, 
                 int *mpierrno)

{
#ifndef HAVE_PAMI_IN_PLACE
  if (sendbuf == MPI_IN_PLACE)
  {
    MPID_Abort (NULL, 0, 1, "'MPI_IN_PLACE' requries support for `PAMI_IN_PLACE`");
    return -1;
  }
#endif
   MPID_Datatype *dt_null = NULL;
   MPI_Aint true_lb = 0;
   int dt_contig ATTRIBUTE((unused)), tsize;
   int mu;
   char *sbuf, *rbuf;
   pami_data_function pop;
   pami_type_t pdt;
   int rc;
   int alg_selected = 0;
   const int rank = comm_ptr->rank;
#if ASSERT_LEVEL==0
   /* We can't afford the tracing in ndebug/performance libraries */
    const unsigned verbose = 0;
#else
    const unsigned verbose = (MPIDI_Process.verbose >= MPIDI_VERBOSE_DETAILS_ALL) && (rank == 0);
#endif
   const struct MPIDI_Comm* const mpid = &(comm_ptr->mpid);
   const int selected_type = mpid->user_selected_type[PAMI_XFER_REDUCE];

   rc = MPIDI_Datatype_to_pami(datatype, &pdt, op, &pop, &mu);
   if(unlikely(verbose))
      fprintf(stderr,"reduce - rc %u, root %u, count %d, dt: %p, op: %p, mu: %u, selectedvar %u != %u (MPICH) sendbuf %p, recvbuf %p\n",
	      rc, root, count, pdt, pop, mu, 
	      (unsigned)selected_type, MPID_COLL_USE_MPICH,sendbuf, recvbuf);

   pami_xfer_t reduce;
   pami_algorithm_t my_reduce=0;
   const pami_metadata_t *my_md = (pami_metadata_t *)NULL;
   int queryreq = 0;
   volatile unsigned reduce_active = 1;

   MPIDI_Datatype_get_info(count, datatype, dt_contig, tsize, dt_null, true_lb);
   rbuf = (char *)recvbuf + true_lb;
   sbuf = (char *)sendbuf + true_lb;
   if(sendbuf == MPI_IN_PLACE) 
   {
      if(unlikely(verbose))
	fprintf(stderr,"reduce MPI_IN_PLACE send buffering (%d,%d)\n",count,tsize);
      sbuf = PAMI_IN_PLACE;
   }

   reduce.cb_done = reduce_cb_done;
   reduce.cookie = (void *)&reduce_active;
   if(mpid->optreduce) /* GLUE_ALLREDUCE */
   {
      char* tbuf = NULL;
      if(unlikely(verbose))
         fprintf(stderr,"Using protocol GLUE_ALLREDUCE for reduce (%d,%d)\n",count,tsize);
      MPIDI_Update_last_algorithm(comm_ptr, "REDUCE_OPT_ALLREDUCE");
      void *destbuf = recvbuf;
      if(rank != root) /* temp buffer for non-root destbuf */
      {
         tbuf = destbuf = MPL_malloc(tsize);
      }
      /* Switch to comm->coll_fns->fn() */
      MPIDO_Allreduce(sendbuf,
                      destbuf,
                      count,
                      datatype,
                      op,
                      comm_ptr,
                      mpierrno);
      if(tbuf)
         MPL_free(tbuf);
      return 0;
   }
   if(selected_type == MPID_COLL_USE_MPICH || rc != MPI_SUCCESS)
   {
      if(unlikely(verbose))
         fprintf(stderr,"Using MPICH reduce algorithm\n");
#if CUDA_AWARE_SUPPORT
      if(MPIDI_Process.cuda_aware_support_on)
      {
         MPI_Aint dt_extent;
         MPID_Datatype_get_extent_macro(datatype, dt_extent);
         char *scbuf = NULL;
         char *rcbuf = NULL;
         int is_send_dev_buf = MPIDI_cuda_is_device_buf(sendbuf);
         int is_recv_dev_buf = MPIDI_cuda_is_device_buf(recvbuf);
         if(is_send_dev_buf)
         {
           scbuf = MPL_malloc(dt_extent * count);
           cudaError_t cudaerr = CudaMemcpy(scbuf, sendbuf, dt_extent * count, cudaMemcpyDeviceToHost);
           if (cudaSuccess != cudaerr) 
             fprintf(stderr, "cudaMemcpy failed: %s\n", CudaGetErrorString(cudaerr));
         }
         else
           scbuf = sendbuf;
         if(is_recv_dev_buf)
         {
           rcbuf = MPL_malloc(dt_extent * count);
           if(sendbuf == MPI_IN_PLACE)
           {
             cudaError_t cudaerr = CudaMemcpy(rcbuf, recvbuf, dt_extent * count, cudaMemcpyDeviceToHost);
             if (cudaSuccess != cudaerr)
               fprintf(stderr, "cudaMemcpy failed: %s\n", CudaGetErrorString(cudaerr));
           }
           else
             memset(rcbuf, 0, dt_extent * count);
         }
         else
           rcbuf = recvbuf;
         int cuda_res =  MPIR_Reduce(scbuf, rcbuf, count, datatype, op, root, comm_ptr, mpierrno);
         if(is_send_dev_buf)MPL_free(scbuf);
         if(is_recv_dev_buf)
         {
           cudaError_t cudaerr = CudaMemcpy(recvbuf, rcbuf, dt_extent * count, cudaMemcpyHostToDevice);
           if (cudaSuccess != cudaerr)
             fprintf(stderr, "cudaMemcpy failed: %s\n", CudaGetErrorString(cudaerr));
           MPL_free(rcbuf);
         }
         return cuda_res;
      }
      else
#endif
      return MPIR_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm_ptr, mpierrno);
   }

   if(selected_type == MPID_COLL_OPTIMIZED)
   {
      if((mpid->cutoff_size[PAMI_XFER_REDUCE][0] == 0) || 
          (mpid->cutoff_size[PAMI_XFER_REDUCE][0] >= tsize && mpid->cutoff_size[PAMI_XFER_REDUCE][0] > 0))
      {
        TRACE_ERR("Optimized Reduce (%s) was pre-selected\n",
         mpid->opt_protocol_md[PAMI_XFER_REDUCE][0].name);
        my_reduce    = mpid->opt_protocol[PAMI_XFER_REDUCE][0];
        my_md = &mpid->opt_protocol_md[PAMI_XFER_REDUCE][0];
        queryreq     = mpid->must_query[PAMI_XFER_REDUCE][0];
      }

   }
   else
   {
      TRACE_ERR("Optimized reduce (%s) was specified by user\n",
      mpid->user_metadata[PAMI_XFER_REDUCE].name);
      my_reduce    =  mpid->user_selected[PAMI_XFER_REDUCE];
      my_md = &mpid->user_metadata[PAMI_XFER_REDUCE];
      queryreq     = selected_type;
   }
   reduce.algorithm = my_reduce;
   reduce.cmd.xfer_reduce.sndbuf = sbuf;
   reduce.cmd.xfer_reduce.rcvbuf = rbuf;
   reduce.cmd.xfer_reduce.stype = pdt;
   reduce.cmd.xfer_reduce.rtype = pdt;
   reduce.cmd.xfer_reduce.stypecount = count;
   reduce.cmd.xfer_reduce.rtypecount = count;
   reduce.cmd.xfer_reduce.op = pop;
   reduce.cmd.xfer_reduce.root = MPIDI_Task_to_endpoint(MPID_VCR_GET_LPID(comm_ptr->vcr, root), 0);


   if(unlikely(queryreq == MPID_COLL_ALWAYS_QUERY || 
               queryreq == MPID_COLL_CHECK_FN_REQUIRED))
   {
      metadata_result_t result = {0};
      TRACE_ERR("Querying reduce protocol %s, type was %d\n",
                my_md->name,
                queryreq);
      if(my_md->check_fn == NULL)
      {
         /* process metadata bits */
         if((!my_md->check_correct.values.inplace) && (sendbuf == MPI_IN_PLACE))
            result.check.unspecified = 1;
         if(my_md->check_correct.values.rangeminmax)
         {
            MPI_Aint data_true_lb ATTRIBUTE((unused));
            MPID_Datatype *data_ptr;
            int data_size, data_contig ATTRIBUTE((unused));
            MPIDI_Datatype_get_info(count, datatype, data_contig, data_size, data_ptr, data_true_lb); 
            if((my_md->range_lo <= data_size) &&
               (my_md->range_hi >= data_size))
               ; /* ok, algorithm selected */
            else
            {
               result.check.range = 1;
               if(unlikely(verbose))
               {   
                  fprintf(stderr,"message size (%u) outside range (%zu<->%zu) for %s.\n",
                          data_size,
                          my_md->range_lo,
                          my_md->range_hi,
                          my_md->name);
               }
            }
         }
      }
Example #7
0
// main search loop
int MainCaitra::prefix_matching_search( float max_time, float threshold ) {
    double start_time = get_wall_time();

    // intialize search - initial back transition (state in prefix matching search)
    BackTransition initialBack( 0.0, 0, 0, -1, 0, NULL);
    // ... associated with initial hypothesis
    states[0].back.push_back( initialBack );
    // start search with maximum error 0, then increase maximum error one by one
    int errorAllowed = 0;

    while( errorAllowed <= prefix.size() * error_unit ){
        // printf("error level %d\n",errorAllowed);
        // process decoder search graph, it is ordered, so we can just sequentially loop through states
        int valid_states = 0;
        int back_count = 0;
        int transition_count = 0;
        int match_count = 0;
        for( int state = 0; state < states.size(); state++ ) {

            // ignore state if it is too bad
            if (threshold > 0 && states[state].best_score < states[0].best_score+threshold) {
                continue;
            }
            valid_states++;
            // abort search if maximum time exceeded
            if (state % 100 == 0 && max_time > 0 && (get_wall_time()-start_time) > max_time) {
                return -1;
            }

            // if it has back transitions, it is reachable, so we have to process each
            for ( backIter back = states[state].back.begin(); back != states[state].back.end(); back++ ) {
                // only need to process back transitions with current error level
                // the ones with lower error have been processed in previous iteration

                /*************************/
                if (back->error == errorAllowed) {
                    back_count++;
                    // loop through transitions out of this state
                    for ( transIter transition = states[state].transitions.begin(); transition != states[state].transitions.end(); transition++ ) {

                        if (threshold > 0 && states[transition->to_state].best_score < states[0].best_score+threshold) {
                            continue;
                        }
                        transition_count++;

                        // try to match this transition's phrase
                        // starting at end word prefix position of previous back transition
                        vector< Match > matches = string_edit_distance( back->prefix_matched, transition->output );
                        // process all matches
                        for ( matchIter match = matches.begin(); match != matches.end(); match++ ) {
                            match_count++;
                            // check if match leads to valid new back transition
                            process_match( state, *back, *match, *transition );

                        }
                    }
                }
            }
        }
        TRACE_ERR("explored " << valid_states << " valid states, " << back_count << " backs, " <<  transition_count << " transitions, " << match_count << " matches at error level " << errorAllowed << endl);
          // found a completion -> we are done
        if (best[errorAllowed].from_state != -1) {
            cerr << "search took " << (get_wall_time()-start_time) << " seconds.\n";
            return errorAllowed;
        }
        errorAllowed++;
    }

    // cout << discover_Error << endl;
    return errorAllowed;
}
Example #8
0
static void aux_process_driver(struct event_entry *entry, struct queue_entry *q)
{
    struct aux_entry *e = (struct aux_entry*)entry;
    NEOERR *err;
    int ret;
    
    mdb_conn *db = e->db;
    struct cache *cd = e->cd;
    struct aux_stats *st = &(e->st);

    st->msg_total++;
    
    mtc_dbg("process cmd %u", q->operation);
    switch (q->operation) {
        CASE_SYS_CMD(q->operation, q, e->cd, err);
    case REQ_CMD_CMT_GET:
        err = aux_cmd_cmtget(q, cd, db);
        break;
    case REQ_CMD_CMT_ADD:
        err = aux_cmd_cmtadd(q, cd, db);
        break;
    case REQ_CMD_CMT_DEL:
        err = aux_cmd_cmtdel(q, cd, db);
        break;
    case REQ_CMD_MAIL_ADD:
        err = aux_cmd_mailadd(q, cd, db);
        break;
    case REQ_CMD_IMP_GET:
        err = aux_cmd_impget(q, cd, db);
        break;
    case REQ_CMD_IMP_ADD:
        err = aux_cmd_impadd(q, cd, db);
        break;
    case REQ_CMD_IMP_DETAIL:
        err = aux_cmd_impdetail(q, cd, db);
        break;
    case REQ_CMD_STATS:
        st->msg_stats++;
        err = STATUS_OK;
        hdf_set_int_value(q->hdfsnd, "msg_total", st->msg_total);
        hdf_set_int_value(q->hdfsnd, "msg_unrec", st->msg_unrec);
        hdf_set_int_value(q->hdfsnd, "msg_badparam", st->msg_badparam);
        hdf_set_int_value(q->hdfsnd, "msg_stats", st->msg_stats);
        hdf_set_int_value(q->hdfsnd, "proc_suc", st->proc_suc);
        hdf_set_int_value(q->hdfsnd, "proc_fai", st->proc_fai);
        break;
    default:
        st->msg_unrec++;
        err = nerr_raise(REP_ERR_UNKREQ, "unknown command %u", q->operation);
        break;
    }
    
    NEOERR *neede = mcs_err_valid(err);
    ret = neede ? neede->error : REP_OK;
    if (PROCESS_OK(ret)) {
        st->proc_suc++;
    } else {
        st->proc_fai++;
        if (ret == REP_ERR_BADPARAM) {
            st->msg_badparam++;
        }
        TRACE_ERR(q, ret, err);
    }
    if (q->req->flags & FLAGS_SYNC) {
        reply_trigger(q, ret);
    }
}
Example #9
0
static void aux_process_driver(EventEntry *entry, QueueEntry *q)
{
    struct aux_entry *e = (struct aux_entry*)entry;
    NEOERR *err;
    int ret;
    
    struct aux_stats *st = &(e->st);

    st->msg_total++;
    
    mtc_dbg("process cmd %u", q->operation);
    switch (q->operation) {
        CASE_SYS_CMD(q->operation, q, e->cd, err);
    case REQ_CMD_CMT_GET:
        err = aux_cmd_cmtget(e, q);
        break;
    case REQ_CMD_CMT_ADD:
        err = aux_cmd_cmtadd(e, q);
        break;
    case REQ_CMD_CMT_DEL:
        err = aux_cmd_cmtdel(e, q);
        break;
    case REQ_CMD_MEMORY_GET:
        err = aux_cmd_memoryget(e, q);
        break;
    case REQ_CMD_MEMORY_ADD:
        err = aux_cmd_memoryadd(e, q);
        break;
    case REQ_CMD_MEMORY_MOD:
        err = aux_cmd_memorymod(e, q);
        break;
    case REQ_CMD_AUX_EMAIL_ADD:
        err = aux_cmd_emailadd(e, q);
        break;
    case REQ_CMD_AUX_INBOX_ADD:
        err = aux_cmd_inboxadd(e, q);
        break;
    case REQ_CMD_STATS:
        st->msg_stats++;
        err = STATUS_OK;
        hdf_set_int_value(q->hdfsnd, "msg_total", st->msg_total);
        hdf_set_int_value(q->hdfsnd, "msg_unrec", st->msg_unrec);
        hdf_set_int_value(q->hdfsnd, "msg_badparam", st->msg_badparam);
        hdf_set_int_value(q->hdfsnd, "msg_stats", st->msg_stats);
        hdf_set_int_value(q->hdfsnd, "proc_suc", st->proc_suc);
        hdf_set_int_value(q->hdfsnd, "proc_fai", st->proc_fai);
        break;
    default:
        st->msg_unrec++;
        err = nerr_raise(REP_ERR_UNKREQ, "unknown command %u", q->operation);
        break;
    }
    
    NEOERR *neede = mcs_err_valid(err);
    ret = neede ? neede->error : REP_OK;
    if (PROCESS_OK(ret)) {
        st->proc_suc++;
    } else {
        st->proc_fai++;
        if (ret == REP_ERR_BADPARAM) {
            st->msg_badparam++;
        }
        TRACE_ERR(q, ret, err);
    }
    if (q->req->flags & FLAGS_SYNC) {
            reply_trigger(q, ret);
    }
}
Example #10
0
/* Create a new context as an environment for GPGME crypto
   operations.  */
gpgme_error_t
gpgme_new (gpgme_ctx_t *r_ctx)
{
  gpgme_error_t err;
  gpgme_ctx_t ctx;
  TRACE_BEG (DEBUG_CTX, "gpgme_new", r_ctx);

  if (_gpgme_selftest)
    return TRACE_ERR (_gpgme_selftest);

  if (!r_ctx)
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));

  ctx = calloc (1, sizeof *ctx);
  if (!ctx)
    return TRACE_ERR (gpg_error_from_syserror ());

  INIT_LOCK (ctx->lock);

  err = _gpgme_engine_info_copy (&ctx->engine_info);
  if (!err && !ctx->engine_info)
    err = gpg_error (GPG_ERR_NO_ENGINE);
  if (err)
    {
      free (ctx);
      return TRACE_ERR (err);
    }

  ctx->keylist_mode = GPGME_KEYLIST_MODE_LOCAL;
  ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT;
  ctx->protocol = GPGME_PROTOCOL_OpenPGP;
  ctx->sub_protocol = GPGME_PROTOCOL_DEFAULT;
  _gpgme_fd_table_init (&ctx->fdt);

  LOCK (def_lc_lock);
  if (def_lc_ctype)
    {
      ctx->lc_ctype = strdup (def_lc_ctype);
      if (!ctx->lc_ctype)
	{
          int saved_err = gpg_error_from_syserror ();
	  UNLOCK (def_lc_lock);
	  _gpgme_engine_info_release (ctx->engine_info);
	  free (ctx);
	  return TRACE_ERR (saved_err);
	}
    }
  else
    def_lc_ctype = NULL;

  if (def_lc_messages)
    {
      ctx->lc_messages = strdup (def_lc_messages);
      if (!ctx->lc_messages)
	{
          int saved_err = gpg_error_from_syserror ();
	  UNLOCK (def_lc_lock);
	  if (ctx->lc_ctype)
	    free (ctx->lc_ctype);
	  _gpgme_engine_info_release (ctx->engine_info);
	  free (ctx);
	  return TRACE_ERR (saved_err);
	}
    }
  else
    def_lc_messages = NULL;
  UNLOCK (def_lc_lock);

  *r_ctx = ctx;

  return TRACE_SUC1 ("ctx=%p", ctx);
}
Example #11
0
/* Create a new data buffer filled with LENGTH bytes starting from
   OFFSET within the file FNAME or stream STREAM (exactly one must be
   non-zero).  */
gpgme_error_t
gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname,
			      FILE *stream, off_t offset, size_t length)
{
#if defined (HAVE_W32CE_SYSTEM) && defined (_MSC_VER)
  return gpgme_error (GPG_ERR_NOT_IMPLEMENTED);
#else
  gpgme_error_t err;
  char *buf = NULL;
  int res;

  TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_filepart", r_dh,
	      "file_name=%s, stream=%p, offset=%lli, length=%u",
	      fname, stream, offset, length);

  if (stream && fname)
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));

  if (fname)
    stream = fopen (fname, "rb");
  if (!stream)
    return TRACE_ERR (gpg_error_from_errno (errno));

#ifdef HAVE_FSEEKO
  res = fseeko (stream, offset, SEEK_SET);
#else
  /* FIXME: Check for overflow, or at least bail at compilation.  */
  res = fseek (stream, offset, SEEK_SET);
#endif

  if (res)
    {
      int saved_errno = errno;
      if (fname)
	fclose (stream);
      return TRACE_ERR (gpg_error_from_errno (saved_errno));
    }

  buf = malloc (length);
  if (!buf)
    {
      int saved_errno = errno;
      if (fname)
	fclose (stream);
      return TRACE_ERR (gpg_error_from_errno (saved_errno));
    }

  while (fread (buf, length, 1, stream) < 1
	 && ferror (stream) && errno == EINTR);
  if (ferror (stream))
    {
      int saved_errno = errno;
      if (buf)
	free (buf);
      if (fname)
	fclose (stream);
      return TRACE_ERR (gpg_error_from_errno (saved_errno));
    }

  if (fname)
    fclose (stream);

  err = gpgme_data_new (r_dh);
  if (err)
    {
      if (buf)
	free (buf);
      return err;
    }

  (*r_dh)->data.mem.buffer = buf;
  (*r_dh)->data.mem.size = length;
  (*r_dh)->data.mem.length = length;

  return TRACE_SUC1 ("r_dh=%p", *r_dh);
#endif
}
Example #12
0
/* This function sets the locale for the context CTX, or the default
   locale if CTX is a null pointer.  */
gpgme_error_t
gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value)
{
  int failed = 0;
  char *new_lc_ctype = NULL;
  char *new_lc_messages = NULL;

  TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx,
	       "category=%i, value=%s", category, value ? value : "(null)");

#define PREPARE_ONE_LOCALE(lcat, ucat)				\
  if (!failed && value						\
      && (category == LC_ALL || category == LC_ ## ucat))	\
    {								\
      new_lc_ ## lcat = strdup (value);				\
      if (!new_lc_ ## lcat)					\
        failed = 1;						\
    }

#ifdef LC_CTYPE
  PREPARE_ONE_LOCALE (ctype, CTYPE);
#endif
#ifdef LC_MESSAGES
  PREPARE_ONE_LOCALE (messages, MESSAGES);
#endif

  if (failed)
    {
      int saved_err = gpg_error_from_syserror ();

      if (new_lc_ctype)
	free (new_lc_ctype);
      if (new_lc_messages)
	free (new_lc_messages);

      return TRACE_ERR (saved_err);
    }

#define SET_ONE_LOCALE(lcat, ucat)			\
  if (category == LC_ALL || category == LC_ ## ucat)	\
    {							\
      if (ctx)						\
	{						\
	  if (ctx->lc_ ## lcat)				\
	    free (ctx->lc_ ## lcat);			\
	  ctx->lc_ ## lcat = new_lc_ ## lcat;		\
	}						\
      else						\
	{						\
	  if (def_lc_ ## lcat)				\
	    free (def_lc_ ## lcat);			\
	  def_lc_ ## lcat = new_lc_ ## lcat;		\
	}						\
    }

  if (!ctx)
    LOCK (def_lc_lock);
#ifdef LC_CTYPE
  SET_ONE_LOCALE (ctype, CTYPE);
#endif
#ifdef LC_MESSAGES
  SET_ONE_LOCALE (messages, MESSAGES);
#endif
  if (!ctx)
    UNLOCK (def_lc_lock);

  return TRACE_ERR (0);
}
Example #13
0
static inline int
MPIDI_Put_use_pami_rput(pami_context_t context, MPIDI_Win_request * req,int *freed)
{
  pami_result_t rc;
  void  *map;
  pami_rput_simple_t params;
  /* params need to zero out to avoid passing garbage to PAMI */
  params=zero_rput_parms;

  params.rma.dest=req->dest;
  params.rma.hints.buffer_registered = PAMI_HINT_ENABLE;
  params.rma.hints.use_rdma          = PAMI_HINT_ENABLE;
  params.rma.bytes   = 0;
  params.rma.cookie  = req;
  params.rma.done_fn = NULL;
  params.rdma.local.mr=&req->origin.memregion;
  params.rdma.remote.mr=&req->win->mpid.info[req->target.rank].memregion;
  params.rdma.remote.offset= req->offset;
  params.put.rdone_fn= MPIDI_Win_DoneCB;

  struct MPIDI_Win_sync* sync = &req->win->mpid.sync;
  TRACE_ERR("Start       index=%u/%d  l-addr=%p  r-base=%p  r-offset=%zu (sync->started=%u  sync->complete=%u)\n",
	    req->state.index, req->target.dt.num_contig, req->buffer, req->win->mpid.info[req->target.rank].base_addr, req->offset, sync->started, sync->complete);
  while (req->state.index < req->target.dt.num_contig) {
    if (sync->started > sync->complete + MPIDI_Process.rma_pending)
      {
	TRACE_ERR("Bailing out;  index=%u/%d  sync->started=%u  sync->complete=%u\n",
		  req->state.index, req->target.dt.num_contig, sync->started, sync->complete);
	return PAMI_EAGAIN;
      }
    ++sync->started;


    params.rma.bytes          =                       req->target.dt.map[req->state.index].DLOOP_VECTOR_LEN;
    params.rdma.remote.offset = req->offset + (size_t)req->target.dt.map[req->state.index].DLOOP_VECTOR_BUF;
    params.rdma.local.offset  = req->state.local_offset;
#ifdef TRACE_ON
    unsigned* buf = (unsigned*)(req->buffer + params.rdma.local.offset);
#endif
    TRACE_ERR("  Sub     index=%u  bytes=%zu  l-offset=%zu  r-offset=%zu  buf=%p  *(int*)buf=0x%08x\n",
	      req->state.index, params.rma.bytes, params.rdma.local.offset, params.rdma.remote.offset, buf, *buf);

    /** sync->total will be updated with every RMA and the complete
	will not change till that RMA has completed. In the meanwhile
	the rest of the RMAs will have memory leaks */
    if (req->target.dt.num_contig - req->state.index == 1) {
         map=NULL;
         if (req->target.dt.map != &req->target.dt.__map) {
             map=(void *) req->target.dt.map;
         }
         rc = PAMI_Rput(context, &params);
         MPID_assert(rc == PAMI_SUCCESS);
         if (map) {
             MPIU_Free(map);
         }
         *freed=1;
         return PAMI_SUCCESS;
    } else {
          rc = PAMI_Rput(context, &params);
          MPID_assert(rc == PAMI_SUCCESS);
          req->state.local_offset += params.rma.bytes;
          ++req->state.index;
    }
  }
  return PAMI_SUCCESS;
}
bool PhraseDictionaryMemory::Load(const std::vector<FactorType> &input
                                  , const std::vector<FactorType> &output
                                  , const string &filePath
                                  , const vector<float> &weight
                                  , size_t tableLimit
                                  , const LMList &languageModels
                                  , float weightWP)
{
    const_cast<LMList&>(languageModels).InitializeBeforeSentenceProcessing();

    const StaticData &staticData = StaticData::Instance();

    m_tableLimit = tableLimit;

    util::FilePiece inFile(filePath.c_str(), staticData.GetVerboseLevel() >= 1 ? &std::cerr : NULL);

    size_t line_num = 0;
    size_t numElement = NOT_FOUND; // 3=old format, 5=async format which include word alignment info
    const std::string& factorDelimiter = staticData.GetFactorDelimiter();

    Phrase sourcePhrase(0);
    std::vector<float> scv;
    scv.reserve(m_numScoreComponent);

    TargetPhraseCollection *preSourceNode = NULL;
    std::string preSourceString;

    while(true) {
        ++line_num;
        StringPiece line;
        try {
            line = inFile.ReadLine();
        } catch (util::EndOfFileException &e) {
            break;
        }

        util::TokenIter<util::MultiCharacter> pipes(line, util::MultiCharacter("|||"));
        StringPiece sourcePhraseString(GrabOrDie(pipes, filePath, line_num));
        StringPiece targetPhraseString(GrabOrDie(pipes, filePath, line_num));
        StringPiece scoreString(GrabOrDie(pipes, filePath, line_num));

        bool isLHSEmpty = !util::TokenIter<util::AnyCharacter, true>(sourcePhraseString, util::AnyCharacter(" \t"));
        if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
            TRACE_ERR( filePath << ":" << line_num << ": pt entry contains empty source, skipping\n");
            continue;
        }

        //target
        std::auto_ptr<TargetPhrase> targetPhrase(new TargetPhrase());
        targetPhrase->CreateFromString(output, targetPhraseString, factorDelimiter);

        scv.clear();
        for (util::TokenIter<util::AnyCharacter, true> token(scoreString, util::AnyCharacter(" \t")); token; ++token) {
            char *err_ind;
            // Token is always delimited by some form of space.  Also, apparently strtod is portable but strtof isn't.
            scv.push_back(FloorScore(TransformScore(static_cast<float>(strtod(token->data(), &err_ind)))));
            if (err_ind == token->data()) {
                stringstream strme;
                strme << "Bad number " << token << " on line " << line_num;
                UserMessage::Add(strme.str());
                abort();
            }
        }
        if (scv.size() != m_numScoreComponent) {
            stringstream strme;
            strme << "Size of scoreVector != number (" <<scv.size() << "!=" <<m_numScoreComponent<<") of score components on line " << line_num;
            UserMessage::Add(strme.str());
            abort();
        }



        size_t consumed = 3;
        if (pipes) {
            targetPhrase->SetAlignmentInfo(*pipes++);
            ++consumed;
        }

        ScoreComponentCollection sparse;
        if (pipes) pipes++; //counts
        if (pipes) {
            //sparse features
            SparsePhraseDictionaryFeature* spdf =
                GetFeature()->GetSparsePhraseDictionaryFeature();
            if (spdf) {
                sparse.Assign(spdf,(pipes++)->as_string());
            }
        }


        // scv good to go sir!
        targetPhrase->SetScore(m_feature, scv, sparse, weight, weightWP, languageModels);

        // Check number of entries delimited by ||| agrees across all lines.
        for (; pipes; ++pipes, ++consumed) {}
        if (numElement != consumed) {
            if (numElement == NOT_FOUND) {
                numElement = consumed;
            } else {
                stringstream strme;
                strme << "Syntax error at " << filePath << ":" << line_num;
                UserMessage::Add(strme.str());
                abort();
            }
        }

        //TODO: Would be better to reuse source phrases, but ownership has to be
        //consistent across phrase table implementations
        sourcePhrase.Clear();
        sourcePhrase.CreateFromString(input, sourcePhraseString, factorDelimiter);
        //Now that the source phrase is ready, we give the target phrase a copy
        targetPhrase->SetSourcePhrase(sourcePhrase);
        if (preSourceString == sourcePhraseString && preSourceNode) {
            preSourceNode->Add(targetPhrase.release());
        } else {
            preSourceNode = CreateTargetPhraseCollection(sourcePhrase);
            preSourceNode->Add(targetPhrase.release());
            preSourceString.assign(sourcePhraseString.data(), sourcePhraseString.size());
        }
    }

    // sort each target phrase collection
    m_collection.Sort(m_tableLimit);

    /* // TODO ASK OLIVER WHY THIS IS NEEDED
    const_cast<LMList&>(languageModels).CleanUpAfterSentenceProcessing();
    */

    return true;
}
// function cpucard_cmd
static int KSAPI cpucard_cmd(ks_reader_dev_t *dev,ks_cpu_cmd_t *cmd,KS_CARD_TYPE t) {
    int ret = -1;
    uint8 sw1,sw2;
    char szCmd[256];
    char ats[200] = {0};
    ks_cpu_cmd_t innerCmd;
    cmd->cmd_retcode=0;
    assert(dev != NULL);
    if(!g_dev_hd) {
        strcpy(gErrMsg,"读卡器端口未打开");
        strcpy(cmd->cmd_retmsg,gErrMsg);
        return KS_PORT_NOTOPEN;
    }

    int recvlen;
    if(t == KS_CPUCARD || t == KS_FIXCARD) {
        // 非接触式
        if(dev->cpuport == CARDSLOT_RF) {
            if(cmd->cmd_type == 0) {
                //1
                //char sendBuf[512] = {0};
                unsigned char cmpBuf[100] = {0};
                int cmpLen = 0;
                char recvBuf[512] = {0};
                int recvLen = 0;
                unsigned char* RevTemp = NULL;
                hex2dec("0084000004", 10, cmpBuf, cmpLen);
                if ( !memcmp(cmpBuf, cmd->send_buf, cmpLen) ) {
                    ret = CPUCARD_APDU(g_dev_hd,"0084000004", recvBuf, gErrMsg);
                } else {
                    //dec2hex(cmd->send_buf, cmd->send_len, sendBuf);
                    ret = CPUCARD_APDU(g_dev_hd, (char*)cmd->send_buf, recvBuf, gErrMsg);
                }
                recvLen = strlen(recvBuf);
                RevTemp = cmd->recv_buf;
                hex2dec((char*)recvBuf, recvLen, cmd->recv_buf, (int&)cmd->recv_len);
                cmd->recv_buf = RevTemp;
            } else {
                //2
                unsigned char ucCmd[512];
                int  iCmdLen=0;
                unsigned char* RevTemp = NULL;
                //hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd,iCmdLen);
                if ( !strncmp("00B09500", (char*)cmd->send_buf, 8) ) {
                    strcat((char*)cmd->send_buf, "14");
                }
                ret = CPUCARD_APDU(g_dev_hd,(char*)cmd->send_buf , (char*)ucCmd, gErrMsg);
                iCmdLen = strlen((char*)ucCmd);
                memcpy(cmd->recv_buf,ucCmd,cmd->recv_len);
                RevTemp = cmd->recv_buf;
                hex2dec((char*)ucCmd, iCmdLen, cmd->recv_buf, (int&)cmd->recv_len);
                cmd->recv_buf = RevTemp;
            }
        } else {
            //GET_DECARD_SLOT(dev,dev->cpuport);
            if(cmd->cmd_type == 0) {
                //3
                char recvBuf[512] = {0};
                int recvLen = 0;
                unsigned char* RevTemp = NULL;
                ret = SAMCAR1_APDU(g_dev_hd, (char*)cmd->send_buf, (char*)recvBuf, gErrMsg);
                recvLen = strlen(recvBuf);
                RevTemp = cmd->recv_buf;
                hex2dec((char*)recvBuf, recvLen, cmd->recv_buf, (int&)cmd->recv_len);
                cmd->recv_buf = RevTemp;
            } else {
                //4
                //unsigned char ucCmd[512];
                //int  iCmdLen=0;
                char recvBuf[512] = {0};
                int recvLen = 0;
                unsigned char* RevTemp = NULL;
                //hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd,iCmdLen);
                ret = SAMCAR1_APDU(g_dev_hd, (char*)cmd->send_buf, (char*)recvBuf, gErrMsg);
                recvLen = strlen(recvBuf);
                RevTemp = cmd->recv_buf;
                hex2dec((char*)recvBuf, recvLen, cmd->recv_buf, (int&)cmd->recv_len);
                cmd->recv_buf = RevTemp;
            }
        }
    } else if(t == KS_SIMCARD) {
#if 0
        unsigned char ucCmd[512];
        int  iCmdLen=0;
        ucCmd[0] = 0xA2;
        ucCmd[1] = 0x33;
        if(cmd->cmd_type == 0) {
            memcpy(ucCmd+2,cmd->send_buf,cmd->send_len);
            iCmdLen = cmd->send_len + 2;
            ret = send_rfsim_cmd(ucCmd,iCmdLen,cmd->recv_buf,(unsigned char*)&recvlen);
            cmd->recv_len=recvlen;
        } else {
            hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd+2,iCmdLen);
            iCmdLen += 2;
            ret = send_rfsim_cmd(ucCmd,iCmdLen,cmd->recv_buf,(unsigned char*)&recvlen);
            cmd->recv_len=recvlen;
        }
#else
        char ucCmd[512] = {0};
        char reCmd[512] = {0};
        int  iCmdLen=0;
        int revLen = 0;
        if ( !strncmp( (char*)cmd->send_buf, "805E000207", 10) ) {
            strcat((char*)cmd->send_buf, "08");
            cmd->send_len+=2;
        }

        if ( !strncmp( (char*)cmd->send_buf, "807400021A", 10) ) {
            strcat((char*)cmd->send_buf, "14");
            cmd->send_len+=2;
        }

        if ( !strncmp( (char*)cmd->send_buf, "807C010212", 10) ) {
            strcat((char*)cmd->send_buf, "2B");
            cmd->send_len+=2;
        }


        if ( !strncmp( (char*)cmd->send_buf, "8476000012", 10) ) {
            strcat((char*)cmd->send_buf, "04");
            cmd->send_len+=2;
        }
        if (cmd->cmd_type == 0) {
            unsigned char* RevTemp = NULL;
            RevTemp = cmd->recv_buf;
            dec2hex(cmd->send_buf, cmd->send_len, ucCmd);
            ret = RFSIMCARD_APDU(g_dev_hd, ucCmd,reCmd, gErrMsg);
            hex2dec(reCmd, strlen(reCmd), cmd->recv_buf, (int&)cmd->recv_len);
            cmd->recv_buf = RevTemp;
        } else {
            unsigned char* RevTemp = NULL;
            RevTemp = cmd->recv_buf;
            if ( !strncmp("00B09500", (char*)cmd->send_buf, 8) ) {
                strcat((char*)cmd->send_buf, "14");
            }
            ret = RFSIMCARD_APDU(g_dev_hd, (char*)cmd->send_buf,reCmd, gErrMsg);
            hex2dec(reCmd, strlen(reCmd), cmd->recv_buf, (int&)cmd->recv_len);
            cmd->recv_buf = RevTemp;
        }
#endif
    } else {
        // 接触式
#if 0	//end del by chen 2012-01-16
        if(dev->cpuport == CARDSLOT_RF) {
            return KS_CMDERROR;
        } else {
#endif	//end del by chen 2012-01-16
            //GET_DECARD_SLOT(dev,dev->cpuport);

            if(cmd->cmd_type == 0) {
                //5
                unsigned char cmpBuf1[100] = {0};
                unsigned char cmpBuf2[100] = {0};
                unsigned char cmpBuf3[100] = {0};
                int cmpLen1 = 0;
                int cmpLen2 = 0;
                int cmpLen3 = 0;
                char recvBuf[512] = {0};
                int recvLen = 0;
                char sendBuf[200] = {0};
                char sendLen = 0;
                unsigned char* RevTemp = NULL;
#if 1
                hex2dec("80FA020020", 10, cmpBuf1, cmpLen1);
                if ( !(memcmp(cmpBuf1, cmd->send_buf, cmpLen1)) ) {

                    dec2hex(cmd->send_buf, 37, sendBuf);
                }
                ret = SAMCAR1_APDU(g_dev_hd,sendBuf , recvBuf, gErrMsg);
                //g_flag = 1;
#else
                hex2dec("80FA0200205F3131313131313131000000000000000000000000001EE6B790201508141000", strlen("80FA0200205F3131313131313131000000000000000000000000001EE6B790201508141000"), cmpBuf1, cmpLen1);
                hex2dec("80FA02002020110814000020001111110007D00013880A12D6870000000000000000000000", strlen("80FA02002020110814000020001111110007D00013880A12D6870000000000000000000000"), cmpBuf2, cmpLen2);
                hex2dec("80FA02002000D5C5C8FD350000000000000000000000000000000000000000000000000000", strlen("80FA02002000D5C5C8FD350000000000000000000000000000000000000000000000000000"), cmpBuf3, cmpLen3);
                if ( !memcmp(cmpBuf1, cmd->send_buf, cmpLen1) ) {
                    ret = CPUCARD_APDU(g_dev_hd, "80FA0200205F3131313131313131000000000000000000000000001EE6B790201508141000", recvBuf, gErrMsg);
                } else if ( !memcmp(cmpBuf2, cmd->send_buf, cmpLen2) ) {
                    ret = CPUCARD_APDU(g_dev_hd, "80FA02002020110814000020001111110007D00013880A12D6870000000000000000000000", recvBuf, gErrMsg);
                } else if ( !memcmp(cmpBuf3, cmd->send_buf, cmpLen3) ) {
                    ret = CPUCARD_APDU(g_dev_hd, recvBuf, "80FA02002000D5C5C8FD350000000000000000000000000000000000000000000000000000", gErrMsg);
                } else {
                    //dec2hex(cmd->send_buf, cmd->send_len, sendBuf);
                    ret = CPUCARD_APDU(g_dev_hd, recvBuf, (char*)cmd->send_buf, gErrMsg);
                }
#endif
                recvLen = strlen(recvBuf);
                RevTemp = cmd->recv_buf;
                hex2dec((char*)recvBuf, recvLen, cmd->recv_buf, (int&)cmd->recv_len);
                cmd->recv_buf = RevTemp;
            } else {
                //6
                //unsigned char ucCmd[512];
                //int  iCmdLen=0;
                char recvBuf[512] = {0};
                int recvLen = 0;
                unsigned char* RevTemp = NULL;
                //hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd,iCmdLen);
                if ( !strncmp((char*)cmd->send_buf, "801A040110", 10) ) {
                    strncpy((char*)cmd->send_buf , "801A440110", 10);
                }
                ret = SAMCAR1_APDU(g_dev_hd, (char*)cmd->send_buf, (char*)recvBuf, gErrMsg);
                recvLen = strlen(recvBuf);
                RevTemp = cmd->recv_buf;
                hex2dec((char*)recvBuf, recvLen, cmd->recv_buf, (int&)cmd->recv_len);
                cmd->recv_buf = RevTemp;
            }
            //}//del by chen 2012-01-16
        }
        if(ret) {
            cmd->cmd_retcode = 0;
            TRACE_ERR("执行指令错误,type["<<t<<"]");
            GetErrMsgByErrCode(ret,gErrMsg);
            strcpy(cmd->cmd_retmsg,gErrMsg);
            return KS_CMDERROR;
        }
        //if(cmd->recv_len < 2)
        //{
        //	cmd->cmd_retcode = 0;
        //	strcpy(cmd->cmd_retmsg,"读卡器读返回数据错误");
        //	cmd->cmd_retcode = 2;
        //	return KS_CMDERROR;
        //}
        sw1 = cmd->recv_buf[cmd->recv_len-2];
        sw2 = cmd->recv_buf[cmd->recv_len-1];
        if(sw1 != 0x90 && sw1 != 0x61) {
            ret = sw1;
            ret = ret*256 + sw2;
            cmd->cmd_retcode = ret;
            TRACE_ERR("执行指令返回状态错误,sw["<<std::hex<<(int)sw1<<" "<<(int)sw2<<"]");
            return cmd->cmd_retcode;
        }
        if(sw1 == 0x61 && sw2 != 0x00) {
            // 61XX ,调用 00C0 取后续数据
            TRACE_ERR("自动获取后续数据");
            memset(&innerCmd,0,sizeof innerCmd);
            innerCmd.send_len = sprintf(szCmd,"00C00000%02X",sw2);
            innerCmd.send_buf = (unsigned char*)szCmd;
            innerCmd.recv_buf = cmd->recv_buf;
            innerCmd.cmd_type = 1; // 16 hex
            ret = cpucard_cmd(dev,&innerCmd,t);
            if(ret==0) {
                cmd->recv_len = innerCmd.recv_len;
            }
            cmd->cmd_retcode = innerCmd.cmd_retcode;
            return ret;
        }
        return 0;
    }

    int KSAPI config_card(ks_reader_dev_t *dev,KS_CPUCARD_TYPE t) {
        return -1;
#if 0
        if(!g_dev_hd)
            return KS_PORT_NOTOPEN;
        if(t == CPUCARD_A)
            return dc_config_card(g_dev_hd,'A');
        else
            return dc_config_card(g_dev_hd,'B');
#endif
    }
    int KSAPI card_type() {
        return g_lastCardType;
    }
    void KSAPI set_logger(void *para) {
        KS_Log_Tracer::instance((KS_Log_Tracer*)para);
    }
Example #16
0
// function cpucard_cmd 
static int KSAPI cpucard_cmd(ks_reader_dev_t *dev,ks_cpu_cmd_t *cmd,KS_CARD_TYPE t)
{
	int ret;
	uint8 sw1,sw2;
	int rlen;
	char szCmd[256];
	ks_cpu_cmd_t innerCmd;
	cmd->cmd_retcode=0;
	assert(dev != NULL);
	if(!g_dev_hd)
	{
		strcpy(gErrMsg,"¶Á¿¨Æ÷¶Ë¿Úδ´ò¿ª");
		strcpy(cmd->cmd_retmsg,gErrMsg);
		return KS_PORT_NOTOPEN;
	}
	
	if(t == KS_SIMCARD)
	{
		// ·Ç½Ó´¥Ê½
		if(dev->cpuport == CARDSLOT_RF)
		{
			if(cmd->cmd_type == 0)
			{
				ret = do_send_recv_rfuim_cmd(cmd->send_buf,cmd->send_len,cmd->recv_buf,rlen,g_default_timeout);
				cmd->recv_len = rlen;
			}
			else
			{
				unsigned char ucCmd[512];
				int  iCmdLen=0;
				hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd,iCmdLen);
				ret = do_send_recv_rfuim_cmd(ucCmd,iCmdLen,cmd->recv_buf,rlen,g_default_timeout);
				cmd->recv_len = rlen;
			}
		}
		else
		{
			return KS_NOTSUPPORT;
		}
	}
	else if(t == KS_CPUCARD || t == KS_FIXCARD)
	{
		// ·Ç½Ó´¥Ê½
		if(dev->cpuport == CARDSLOT_RF)
		{
			if(cmd->cmd_type == 0)
			{
				ret = do_send_recv_cpucard_cmd(cmd->send_buf,cmd->send_len,cmd->recv_buf,rlen,g_default_timeout);
				cmd->recv_len = rlen;
			}
			else
			{
				unsigned char ucCmd[512];
				int  iCmdLen=0;
				hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd,iCmdLen);
				ret = do_send_recv_cpucard_cmd(ucCmd,iCmdLen,cmd->recv_buf,rlen,g_default_timeout);
				cmd->recv_len = rlen;
			}
		}
		else
		{
			return KS_NOTSUPPORT;
		}
	}
	else if(t == KS_MFCARD)
	{
		// ·Ç½Ó´¥Ê½
		if(dev->cpuport == CARDSLOT_RF)
		{
			if(cmd->cmd_type == 0)
			{
				ret = do_send_recv_pos_cmd(cmd->send_buf,cmd->send_len,cmd->recv_buf,rlen,g_default_timeout);
				cmd->recv_len = rlen;
			}
			else
			{
				unsigned char ucCmd[512];
				int  iCmdLen=0;
				hex2dec((char*)cmd->send_buf,cmd->send_len,ucCmd,iCmdLen);
				ret = do_send_recv_pos_cmd(ucCmd,iCmdLen,cmd->recv_buf,rlen,g_default_timeout);
				cmd->recv_len = rlen;
			}
		}
		else
		{
			return KS_NOTSUPPORT;
		}
	}
	else
	{
		// ½Ó´¥Ê½
		return KS_NOTSUPPORT;
	}
	if(ret<0 && ret > -3)
	{
		cmd->cmd_retcode = 0;
		GetErrMsgByErrCode(ret,gErrMsg);
		strcpy(cmd->cmd_retmsg,gErrMsg);
		return KS_CMDERROR;
	}

	sw1 = cmd->recv_buf[cmd->recv_len-2];
	sw2 = cmd->recv_buf[cmd->recv_len-1];
	if(sw1 != 0x90 && sw1 != 0x61)
	{
		ret = sw1;
		ret = ret*256 + sw2;
		cmd->cmd_retcode = ret;
		return cmd->cmd_retcode;
	}
	if(sw1 == 0x61 && sw2 != 0x00)
	{
		TRACE_ERR("×Ô¶¯·¢ÆðÈ¡ºóÐøÊý¾ÝÇëÇó");
		// 61XX ,µ÷Óà 00C0 È¡ºóÐøÊý¾Ý
		memset(&innerCmd,0,sizeof innerCmd);
		innerCmd.send_len = sprintf(szCmd,"00C00000%02X",sw2);
		innerCmd.send_buf = (unsigned char*)szCmd;
		innerCmd.recv_buf = cmd->recv_buf;
		innerCmd.cmd_type = 1; // 16 hex
		ret = cpucard_cmd(dev,&innerCmd,t);
		if(ret==0)
		{
			cmd->recv_len = innerCmd.recv_len;
		}
		cmd->cmd_retcode = innerCmd.cmd_retcode;
		return ret;
	}
	return 0;
}
Example #17
0
static void reduce_cb_done(void *ctxt, void *clientdata, pami_result_t err)
{
   unsigned *active = (unsigned *)clientdata;
   TRACE_ERR("cb_reduce enter, active: %u\n", (*active));
   (*active)--;
}
Example #18
0
// function request_card 
static int KSAPI request_card(ks_reader_dev_t *dev,char* phyid,KS_CARD_TYPE t)
{
	int ret,i;
	unsigned long snr;
	unsigned short tagtype;
	unsigned char size,sw1,sw2;
	uint8 ucCardPhyID[8];
	uint8 rData[256];
	uint8 cmd[256];
	int len,rlen,retries;
	//g_lastCardType = 0;
	gErrMsg[0]=0;
	if(!g_dev_hd)
	{
		ret=open_device(dev);
		if(ret)
			return ret;
	}
	if(!g_dev_hd)
	{
		return KS_PORT_NOTOPEN;
	}
	/*
	if(KS_SIMCARD ==  t )
	{
		if(KS_SIMCARD == g_lastCardType || 0 == g_lastCardType)
		{
			// ÉÏ´ÎÑ°¿¨ÊÇÊÖ»ú¿¨£¬²»ÓÃreset
		}
		if(dev->cpuport == CARDSLOT_RF)
		{
			len = 5;
			retries = 10;
			while(--retries > 0)
			{
				memset(rData,0,sizeof(rData));
				memcpy(cmd,"\x90\xB0\x04\x00\x00",5);
				if(do_send_recv_pos_cmd(cmd,5,rData,rlen,300))
				{
					if(rData[0] == 0x9C && rData[1] == 0x02)
					{
						// read success
						break;
					}
					else if(rData[0] == 0x9C && rData[1] == 0x03)
					{

						Sleep(100); // 100ms
					}
				}
			}
			if(retries == 0)
			{
				TRACE_ERR("¼ì²âRFUIM¿¨³¬Ê±");
				return KS_REQUESTCARD;
			}
			if((rData[2] & 0xF0) != 0x10) // 2.4 G ¿¨
			{
				TRACE_ERR("¼ì²âRFUIM״̬´íÎó,code["<<(int)rData[2]<<"]");
				return KS_REQUESTCARD;
			}
			memcpy(ucCardPhyID,rData+3,8); // physical id
			dec2hex(ucCardPhyID,8,phyid);
			phyid[16]=0;
			dev->cardtype = KS_SIMCARD;
			g_lastCardType = dev->cardtype;
			return 0;
		}
		else
		{
			TRACE_ERR("·¢ËÍÊÖ»ú¿¨Ñ°¿¨Ö¸Áî,²ÎÊý´íÎó");
			return KS_REQUESTCARD;
		}
	}
	else if(KS_MFCARD ==t || KS_CPUCARD == t || KS_FIXCARD == t)
	{
		len = 5;
		retries = 3;
		if(KS_SIMCARD == g_lastCardType || 0 == g_lastCardType)
		{
			// ÉÏ´ÎÑ°¿¨ÊÇÊÖ»ú¿¨£¬²»ÓÃreset
		}
		else
		{
			reset(dev,300);
		}
		while(--retries > 0)
		{
			memcpy(cmd,"\x90\xB0\x04\x52\x00",5);
			memset(rData,0,sizeof(rData));
			if(do_send_recv_pos_cmd(cmd,5,rData,rlen,300))
			{
				if(rData[0] == 0x9C && rData[1] == 0x02)
				{
					// read success
				}
				else if(rData[0] == 0x9C && rData[1] == 0x03)
				{
					Sleep(100); 
					continue;
				}
			}
			else
			{
				TRACE_ERR("¼ì²âIC¶Á¿¨Æ÷ÎÞÓ¦´ð");
				return KS_REQUESTCARD;
			}

			if(rData[2] != 0x45)
			{
				TRACE_ERR("¼ì²âIC״̬´íÎócode["<<(int)rData[2]<<"]");
				return KS_REQUESTCARD;
			}
			if(memcmp(rData+7,"\x04\x00",2) == 0
				|| memcmp(rData+7,"\x02\x00",2) == 0)
			{
				// m1 ¿¨
				if(rData[9] != 0x08)
				{
					// 7+1 ¿¨
					dev->cardtype = KS_FIXCARD;
				}
				else
				{
					dev->cardtype = KS_MFCARD;
				}
				break;
			}
			else if(memcmp(rData+7,"\x08\x00",2) == 0)
			{
				// CPU ¿¨
				dev->cardtype = KS_CPUCARD;
			}
			else 
			{
				TRACE_ERR("¼ì²âIC¿¨Ê±£¬ÎÞ·¨Ê¶±ð¿¨ÀàÐÍ");
				return KS_REQUESTCARD;
			}
		}
		if(retries == 0)
		{
			TRACE_ERR("¼ì²âIC¿¨³¬Ê±");
			return KS_REQUESTCARD;
		}
		// read success
		for(i = 0;i < 4; ++i)
			ucCardPhyID[3-i] = rData[3+i]; // physical id

		if(dev->cardtype == KS_FIXCARD || dev->cardtype == KS_CPUCARD)
		{
			// CPU ¿¨reset
			if(t == KS_CPUCARD || t == KS_FIXCARD)
			{
				len = 5;
				memcpy(cmd,"\x40\x50\x00\x00\x00",len);
				if(do_send_recv_cpu_cmd(cmd,len,rData,rlen,300))
				{
					TRACE_ERR("¼ì²âIC¿¨ÎªCPU¿¨£¬CPU¿¨¸´Î»´íÎó");
					return KS_REQUESTCARD;
				}
			}
		}		
		dec2hex(ucCardPhyID,4,phyid);
		phyid[8] = 0;
		g_lastCardType = dev->cardtype;
		return 0;
	}
	*/
	if(g_lastCardType != 0 ) //&& g_lastCardType != KS_SIMCARD && g_lastCardType != KS_MFCARD)
	{
		reset(dev,300);
	}
	len = 5;
	retries = 3;
	while(--retries > 0)
	{
		memcpy(cmd,"\x90\xB0\x04\x52\x00",5);
		memset(rData,0,sizeof(rData));
		if(do_send_recv_pos_cmd(cmd,5,rData,rlen,300))
		{
			if(rData[0] == 0x9C && rData[1] == 0x02)
			{
				// read success
			}
			else if(rData[0] == 0x9C && rData[1] == 0x03)
			{
				Sleep(100); 
				continue;
			}
		}
		else
		{
			TRACE_ERR("¼ì²âIC¶Á¿¨Æ÷ÎÞÓ¦´ð");
			return KS_REQUESTCARD;
		}
		// Èç¹ûÊÇÊÖ»ú¿¨
		if(rData[2] != 0x45)
		{
			// SIMCARD
			memcpy(ucCardPhyID,rData+3,8); // physical id
			dec2hex(ucCardPhyID,8,phyid);
			phyid[16]=0;
			dev->cardtype = KS_SIMCARD;
			g_lastCardType = dev->cardtype;
			return 0;
		}
		else
		{
			if(memcmp(rData+7,"\x04\x00",2) == 0
				|| memcmp(rData+7,"\x02\x00",2) == 0)
			{
				// m1 ¿¨
				if(rData[9] == 0x08)
				{
					// mifare one
					dev->cardtype = KS_MFCARD;
				}
				else if(rData[9] == 0x18)
				{
					// mifare one
					//if(memcmp(rData+7,"\x02\x00",2) == 0)  S70
					dev->cardtype = KS_MFCARD;
				}
				else if(rData[9] != 0x28)
				{
					// 7+1 ¿¨
					dev->cardtype = KS_FIXCARD;
				}
				else
				{
					dev->cardtype = KS_CPUCARD;
				}
			}
			else if(memcmp(rData+7,"\x08\x00",2) == 0)
			{
				// CPU ¿¨
				dev->cardtype = KS_CPUCARD;
			}
			else 
			{
				TRACE_ERR("¼ì²âIC¿¨Ê±£¬ÎÞ·¨Ê¶±ð¿¨ÀàÐÍ");
				return KS_REQUESTCARD;
			}
			// read success
			for(i = 0;i < 4; ++i)
				ucCardPhyID[3-i] = rData[3+i]; // physical id
			dec2hex(ucCardPhyID,4,phyid);
			phyid[8] = 0;
			g_lastCardType = dev->cardtype;
			return 0;
		}
	}
	if(retries == 0)
	{
		TRACE_ERR("¼ì²âIC¿¨³¬Ê±");
		return KS_REQUESTCARD;
	}
	
	
	TRACE_ERR("´«Èë²ÎÊýÓÐÎó");
	return KS_REQUESTCARD;
}
Example #19
0
void  MainCaitra::createGraph(vector<SearchGraphNode> v) {

    map<int, int> recombination;
    for (int i = 0; i < v.size(); i++) {
        int id = v[i].hypo->GetId();

        int toState;
        int forward = v[i].forward;
        float fscore = v[i].fscore;

        if(id == 0){
            State newState = State(forward, fscore, fscore);
            states.push_back(newState);
            stateId2hypId.push_back(id);
            continue;
        }

        const Hypothesis *prevHypo = v[i].hypo->GetPrevHypo();
        int fromState = prevHypo->GetId();
        float backwardScore = v[i].hypo->GetScore();
        float transitionScore = (v[i].hypo->GetScore() - prevHypo->GetScore());

        int recombined = -1;
        if (v[i].recombinationHypo != NULL)
            recombined = v[i].recombinationHypo->GetId();

        string out = v[i].hypo->GetCurrTargetPhrase().GetStringRep(StaticData::Instance().GetOutputFactorOrder());


        if (recombined >= 0) {
            recombination[ id ] = recombined;
            toState = recombined;
        }
        else {
            toState = id;
        }

        vector<Word> o = tokenize( out, backwardScore + fscore );
        Transition newTransition( toState, transitionScore, o);
        int thisKey = hypId2stateId[ fromState ];
        states[ thisKey ].transitions.push_back( newTransition );

        if (recombined == -1) {

            State newState(forward, fscore, backwardScore+fscore );
            states.push_back( newState );
            hypId2stateId[ id ] = stateId2hypId.size();
            stateId2hypId.push_back( id );
        }

    }

    hypId2stateId[-1]=-1;

    for(int state=0; state<states.size(); state++) {
        int forward = states[state].forward;
        if (recombination.count(forward)) {
            forward = recombination[ forward ];
        }
        states[state].forward = hypId2stateId[ forward ];
        for ( transIter transition = states[state].transitions.begin(); transition != states[state].transitions.end(); transition++ ) {
            transition->to_state = hypId2stateId[ transition->to_state ];
        }

    }
    transitionsSize = v.size();

    TRACE_ERR("graph has " << states.size() << " states, pruned down from " << v.size() << "\n");


}
Example #20
0
int usbsim_testdev_init(USBSIM_TESTDEV * td)
{
	USBSIM_DEV_PARAMS params;
	USB_DEVICE_DESC device_desc;
	struct {
		USB_CONFIGURATION_DESC config;
		USB_INTERFACE_DESC intf;
		USB_ENDPOINT_DESC ep[2];
	} config_desc;
	int ret, index;

	/* Initialize test device structure */
	memset(td, 0, sizeof(USBSIM_TESTDEV));
	xpcf_list_init(&td->data_list);

	/* Create simulated device */
	memset(&params, 0, sizeof(params));
	params.speed = USBSIM_HIGH_SPEED;
	params.mps0 = HS_CTRL_MPS;
	params.ctx = td;
	params.sts_change_handler = usbsim_testdev_sts_change;
	ret = usbsim_dev_create(&params, &td->dev);
	if (ret) {
		TRACE_ERR("usbsim_dev_create", ret);
		return ret;
	}

	/* Initialize device descriptor */
	memset(&device_desc, 0, sizeof(device_desc));
	device_desc.bLength = sizeof(device_desc);
	device_desc.bDescriptorType = USB_DESC_TYPE_DEVICE;
	device_desc.bDeviceClass = 0xFF;
	device_desc.bcdUSB = 0x0200;
	device_desc.bMaxPacketSize0 = HS_CTRL_MPS;
	device_desc.idVendor = DEV_VID;
	device_desc.idProduct = DEV_PID;
	device_desc.bcdDevice = DEV_REV;
	device_desc.bNumConfigurations = 1;

	/* Add device descriptor */
	ret =
	    usbsim_dev_add_desc(td->dev, USB_DESC_TYPE_DEVICE, 0, 0,
				&device_desc, sizeof(device_desc));
	if (ret) {
		TRACE_ERR("usbsim_dev_add_desc(DEVICE)", ret);
		goto fail;
	}

	/* Initialize configuration descriptor */
	memset(&config_desc, 0, sizeof(config_desc));
	config_desc.config.bLength = sizeof(config_desc.config);
	config_desc.config.bDescriptorType = USB_DESC_TYPE_CONFIGURATION;
	config_desc.config.wTotalLength = sizeof(config_desc);
	config_desc.config.bNumInterfaces = 1;
	config_desc.config.bConfigurationValue = 1;
	config_desc.config.bmAttributes = 0xC0;

	/* Initialize interface descriptor */
	config_desc.intf.bLength = sizeof(config_desc.intf);
	config_desc.intf.bDescriptorType = USB_DESC_TYPE_INTERFACE;
	config_desc.intf.bNumEndpoints = 2;
	config_desc.intf.bInterfaceClass = 0xFF;

	/* Initialize endpoint descriptor 1 */
	config_desc.ep[0].bLength = sizeof(config_desc.ep[0]);
	config_desc.ep[0].bDescriptorType = USB_DESC_TYPE_ENDPOINT;
	config_desc.ep[0].bEndpointAddress = EP_ADDR1;
	config_desc.ep[0].bmAttributes = 0x02;
	config_desc.ep[0].wMaxPacketSize = HS_BULK_MPS;

	/* Initialize endpoint descriptor 2 */
	config_desc.ep[1].bLength = sizeof(config_desc.ep[1]);
	config_desc.ep[1].bDescriptorType = USB_DESC_TYPE_ENDPOINT;
	config_desc.ep[1].bEndpointAddress = EP_ADDR2;
	config_desc.ep[1].bmAttributes = 0x02;
	config_desc.ep[1].wMaxPacketSize = HS_BULK_MPS;

	/* Add configuration descriptor set */
	ret =
	    usbsim_dev_add_desc(td->dev, USB_DESC_TYPE_CONFIGURATION, 0, 0,
				&config_desc, sizeof(config_desc));
	if (ret) {
		TRACE_ERR("usbsim_dev_add_desc(CONFIGURATION)", ret);
		goto fail;
	}

	/* Add simulated endpoints */
	for (index = 0; index < 2; index++) {
		ret = usbsim_testdev_add_ep(td, &config_desc.ep[index]);
		if (ret)
			goto fail;
	}

	return 0;

 fail:
	usbsim_testdev_term(td);
	return ret;
}
Example #21
0
string MainCaitra::getPrefix(string pr){

    double start_time = get_wall_time();
    prefix_has_final_space = (pr[pr.length()-1] == ' ');
    prefix = tokenize(pr, 0);
    string last_token = surface[prefix[prefix.size()-1]];

    // allow partial matching of last token in prefix matching search
    if (last_word_may_match_partially && !prefix_has_final_space) {
        // also allow case-insensitive match
        string last_token_lowercase = lowercase(last_token);

        partially_matches_last_token.clear();
        // for all words in vocabulary
        for (map<string, Word>::iterator iter = lexicon.begin(); iter != lexicon.end(); iter++) {
            string word = lowercase(iter->first);
            // check if could be a partial match
            if (last_token_lowercase.length() < word.length() &&
                    last_token_lowercase == word.substr(0,last_token.length())) {
                partially_matches_last_token.insert( iter->second );
            }
        }

    }
    // allow case-insensitive matching
    if (case_insensitive_matching) {
        // for all words in prefix
        for(int p=0; p<prefix.size(); p++) {
            if (already_processed.count( prefix[p] )) {
                continue;
            }
            // for all words in vocabulary
            for (map<string, Word>::iterator iter = lexicon.begin(); iter != lexicon.end(); iter++) {
                // if they match case-insensitive, take note
                if (equal_case_insensitive( surface[ prefix[p] ], iter->first )) {
                    lowercase_word_match.insert( make_pair( prefix[p], iter->second ) );
                }
            }
        }
    }

    // consider mismatches of similarly spelled words as half an error
    if (approximate_word_match_threshold < 1.0) {
        // for all words in prefix
        for(int p=0; p<prefix.size(); p++) {
            if (already_processed.count( prefix[p] )) {
                continue;
            }
            int length_prefix_word = surface[ prefix[p] ].size();
            // for all words in vocabulary
            for (map<string, Word>::iterator iter = lexicon.begin(); iter != lexicon.end(); iter++) {
                int distance = letter_string_edit_distance( prefix[p], iter->second );
                int length_vocabulary_word = iter->first.size();
                int min_length = length_prefix_word < length_vocabulary_word ? length_prefix_word : length_vocabulary_word;
                if (distance <= min_length * approximate_word_match_threshold) {
                    approximate_word_match.insert( make_pair( prefix[p], iter->second ) );
                }
            }
        }
    }


    // consider mismatches in word endings (presumably morphological variants) as half an error
    if (suffix_insensitive_max_suffix > 0) {
        // for all words in prefix
        for(int p=0; p<prefix.size(); p++) {
            if (already_processed.count( prefix[p] )) {
                continue;
            }
            // for all words in vocabulary
            int length_prefix_word = surface[ prefix[p] ].size();
            for (map<string, Word>::iterator iter = lexicon.begin(); iter != lexicon.end(); iter++) {
                int length_vocabulary_word = iter->first.size();
                if (abs(length_vocabulary_word-length_prefix_word) <= suffix_insensitive_max_suffix &&
                        length_prefix_word >= suffix_insensitive_min_match &&
                        length_vocabulary_word >= suffix_insensitive_min_match) {
                    int specific_min_match = ( length_prefix_word > length_vocabulary_word ) ? length_prefix_word : length_vocabulary_word;
                    specific_min_match -= suffix_insensitive_max_suffix;
                    if (suffix_insensitive_min_match > specific_min_match) {
                        specific_min_match = suffix_insensitive_min_match;
                    }
                    if (iter->first.substr(0,specific_min_match) ==
                            surface[ prefix[p] ].substr(0,specific_min_match)) {
                        suffix_insensitive_word_match.insert( make_pair( prefix[p], iter->second ) );
                    }
                }
            }
        }
    }


    // record seen words for caching pre-processing across requests
    if (case_insensitive_matching ||
            approximate_word_match_threshold < 1.0 ||
            suffix_insensitive_max_suffix > 0) {
        for(int p=0; p<prefix.size(); p++) {
            if (!already_processed.count( prefix[p] )) {
                already_processed.insert( prefix[p] );
            }
        }
    }
    TRACE_ERR("preparation took " << (get_wall_time() - start_time) << " seconds\n");


    // call the main search loop
    int errorAllowed = prefix_matching_search( max_time, 0 );
    if (max_time>0 && errorAllowed == -1) {
        errorAllowed = prefix_matching_search( 0, 0.000001 );
    }

    if(errorAllowed == 20000)
        return "";
    // we found the best completion, now construct suffix for output
    Best &b = best[errorAllowed];
    vector< Word > matchedPrefix, predictedSuffix;


    // add words from final prediction
    for(int i=b.output_matched-1;i>=0;i--) {
        matchedPrefix.push_back( b.transition->output[i] );
    }
    for(int i=b.output_matched;i<b.transition->output.size();i++) {
        predictedSuffix.push_back( b.transition->output[i] );
    }

    // add suffix words (best path forward)
    int suffixState = b.transition->to_state;
    while (states[suffixState].forward > 0) {
        Transition *transition = NULL;
        float best_score = -999;
        vector< Transition > &transitions = states[suffixState].transitions;
        for(int t=0; t<transitions.size(); t++) {
            if (transitions[t].to_state == states[suffixState].forward &&
                    transitions[t].score > best_score) {
                transition = &transitions[t];
                best_score = transition->score;
            }
        }
        for(int i=0;i<transition->output.size();i++) {
            predictedSuffix.push_back( transition->output[i] );
        }
        suffixState = states[suffixState].forward;
    }

    // add prefix words (following back transitions)
    int prefixState = b.from_state;
    int prefix_matched = b.back_matched;
    while (prefixState > 0) {
        backIter back = states[prefixState].back.begin();
        for(; back != states[prefixState].back.end(); back++ ) {
            if (back->prefix_matched == prefix_matched) {
                break;
            }
        }
        const vector< Word > &output = back->transition->output;
        for(int i=output.size()-1; i>=0; i--) {
            matchedPrefix.push_back( output[i] );
        }
        back->transition->output.size();
        prefixState = back->back_state;
        prefix_matched = back->back_matched;
    }

    string res = "";

    // handle final partial word (normal case)
    bool successful_partial_word_completion = false;
    if (!successful_partial_word_completion && last_word_may_match_partially && !prefix_has_final_space && matchedPrefix.size()>0) {
        Word last_matched_word = matchedPrefix[ 0 ];
        if (partially_matches_last_token.count( last_matched_word )) {
            res += surface[ last_matched_word ].substr(last_token.length());
            successful_partial_word_completion = true;
        }
    }

    // try a bit harder to match the last word
    if (!successful_partial_word_completion && prefix.size()>0 && match_last_word_window>0 &&
            (matchedPrefix.size() == 0 || matchedPrefix[ 0 ] != prefix[prefix.size()-1])) {
        // if we match it case-insensitive, that's okay
        bool is_okay = false;
        if (matchedPrefix.size()>0) {
            if (equal_case_insensitive(last_token, surface[ matchedPrefix[0] ])) {
                is_okay = true;
            }
        }
        // look for last word in window around current matched path position
        for(int i=0; !is_okay && i<=match_last_word_window; i++) {
            // is the word in the predicted suffix?
            if (predictedSuffix.size() > i &&
                    (equal_case_insensitive(last_token, surface[ predictedSuffix[i]]) ||
                     (last_word_may_match_partially && !prefix_has_final_space && partially_matches_last_token.count( predictedSuffix[i] )))) {
                // move predicted suffix words into matched prefix
                for(int j=0; j<=i; j++) {
                    matchedPrefix.insert( matchedPrefix.begin(), predictedSuffix[0] );
                    predictedSuffix.erase( predictedSuffix.begin() );
                }
                is_okay = true;
            }
            // is the word in the macthed prefix?
            else if (i>0 && matchedPrefix.size() > i &&
                     (equal_case_insensitive(last_token, surface[ matchedPrefix[i]]) ||
                      (last_word_may_match_partially && !prefix_has_final_space && partially_matches_last_token.count( matchedPrefix[i] )))) {
                // move matched prefix words into predicted suffix
                for(int j=0; j<i; j++) {
                    predictedSuffix.insert( predictedSuffix.begin(), matchedPrefix[0] );
                    matchedPrefix.erase( matchedPrefix.begin() );
                }
                is_okay = true;
            }
        }
    }


    // desparation word completion: matching word with best path score
    if (match_last_partial_word_desparately && !prefix_has_final_space && !successful_partial_word_completion && word_score[ prefix[prefix.size()-1] ] == 0) {
        string best;
        float best_score = -9e9;
        bool best_case_sensitive = false;
        // for all words in vocabulary

        for (map<string, Word>::iterator iter = lexicon.begin(); iter != lexicon.end(); iter++) {
            // word known in different casing, use it
            if (equal_case_insensitive(iter->first, last_token) &&
                    word_score[ iter->second ] != 0) {
                best = iter->first;
                best_score = 0;
                break;
            }
            if (iter->first.length() >= last_token.length() &&
                    word_score[ iter->second ] != 0 &&
                    word_score[ iter->second ] > best_score &&
                    equal_case_insensitive(iter->first.substr(0,last_token.length()), last_token)) {
                // prefer case-sensitive match
                if (iter->first.substr(0,last_token.length()) == last_token) {
                    best_case_sensitive = true;
                    best = iter->first;
                    best_score = word_score[ iter->second ];
                }
                if (!best_case_sensitive) {
                    best = iter->first;
                    best_score = word_score[ iter->second ];
                }
            }
        }
        if (best_score > -8e9) {
            cout << best.substr(last_token.length());
            successful_partial_word_completion = true;
        }
    }

    // output results
    for( int i=0; i<predictedSuffix.size(); i++) {
        if (i>0 || !prefix_has_final_space) { res += " "; }
        res += surface[ predictedSuffix[i] ];
    }

    // if no prediction, just output space
    if (predictedSuffix.size() == 0) {
        res += " ";
    }

    // clear out search
    for( int state = 0; state < states.size(); state++ ) {
        states[state].back.clear();
    }
    for (int errorAllowed = 0; errorAllowed < 20001; errorAllowed++ ) {
        best[errorAllowed].from_state = -1;
    }

    return res;

}
/** load all parameters from the configuration file and the command line switches */
bool Parameter::LoadParam(int argc, char* argv[])
{
  // config file (-f) arg mandatory
  string configPath;
  if ( (configPath = FindParam("-f", argc, argv)) == ""
       && (configPath = FindParam("-config", argc, argv)) == "") {
    PrintCredit();
    Explain();

    UserMessage::Add("No configuration file was specified.  Use -config or -f");
    return false;
  } else {
    if (!ReadConfigFile(configPath)) {
      UserMessage::Add("Could not read "+configPath);
      return false;
    }
  }

  // overwrite parameters with values from switches
  for(PARAM_STRING::const_iterator iterParam = m_description.begin(); iterParam != m_description.end(); iterParam++) {
    const string paramName = iterParam->first;
    OverwriteParam("-" + paramName, paramName, argc, argv);
  }

  // ... also shortcuts
  for(PARAM_STRING::const_iterator iterParam = m_abbreviation.begin(); iterParam != m_abbreviation.end(); iterParam++) {
    const string paramName = iterParam->first;
    const string paramShortName = iterParam->second;
    OverwriteParam("-" + paramShortName, paramName, argc, argv);
  }

  // logging of parameters that were set in either config or switch
  int verbose = 1;
  if (m_setting.find("verbose") != m_setting.end() &&
      m_setting["verbose"].size() > 0)
    verbose = Scan<int>(m_setting["verbose"][0]);
  if (verbose >= 1) { // only if verbose
    TRACE_ERR( "Defined parameters (per moses.ini or switch):" << endl);
    for(PARAM_MAP::const_iterator iterParam = m_setting.begin() ; iterParam != m_setting.end(); iterParam++) {
      TRACE_ERR( "\t" << iterParam->first << ": ");
      for ( size_t i = 0; i < iterParam->second.size(); i++ )
        TRACE_ERR( iterParam->second[i] << " ");
      TRACE_ERR( endl);
    }
  }

  // check for illegal parameters
  bool noErrorFlag = true;
  for (int i = 0 ; i < argc ; i++) {
    if (isOption(argv[i])) {
      string paramSwitch = (string) argv[i];
      string paramName = paramSwitch.substr(1);
      if (m_valid.find(paramName) == m_valid.end()) {
        UserMessage::Add("illegal switch: " + paramSwitch);
        noErrorFlag = false;
      }
    }
  }

  // check if parameters make sense
  return Validate() && noErrorFlag;
}
Example #23
0
/* This function is similar to pipe_connect but uses a socketpair and
   sets the I/O up to use sendmsg/recvmsg. */
static gpg_error_t
socketpair_connect (assuan_context_t ctx,
                    const char *name, const char **argv,
                    assuan_fd_t *fd_child_list,
                    void (*atfork) (void *opaque, int reserved),
                    void *atforkvalue)
{
  gpg_error_t err;
  int idx;
  int fds[2];
  char mypidstr[50];
  pid_t pid;
  int *child_fds = NULL;
  int child_fds_cnt = 0;
  struct at_socketpair_fork atp;
  int rc;

  TRACE_BEG3 (ctx, ASSUAN_LOG_CTX, "socketpair_connect", ctx,
	      "name=%s,atfork=%p,atforkvalue=%p", name ? name : "(null)",
	      atfork, atforkvalue);

  atp.user_atfork = atfork;
  atp.user_atforkvalue = atforkvalue;
  atp.parent_pid = getpid ();

  if (!ctx
      || (name && (!argv || !argv[0]))
      || (!name && !argv))
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);

  if (! ctx->flags.no_fixsignals)
    fix_signals ();

  sprintf (mypidstr, "%lu", (unsigned long)getpid ());

  if (fd_child_list)
    while (fd_child_list[child_fds_cnt] != ASSUAN_INVALID_FD)
      child_fds_cnt++;
  child_fds = _assuan_malloc (ctx, (child_fds_cnt + 2) * sizeof (int));
  if (! child_fds)
    return TRACE_ERR (gpg_err_code_from_syserror ());
  child_fds[1] = ASSUAN_INVALID_FD;
  if (fd_child_list)
    memcpy (&child_fds[1], fd_child_list, (child_fds_cnt + 1) * sizeof (int));

  if (_assuan_socketpair (ctx, AF_LOCAL, SOCK_STREAM, 0, fds))
    {
      TRACE_LOG1 ("socketpair failed: %s", strerror (errno));
      _assuan_free (ctx, child_fds);
      return TRACE_ERR (GPG_ERR_ASS_GENERAL);
    }
  atp.peer_fd = fds[1];
  child_fds[0] = fds[1];

  rc = _assuan_spawn (ctx, &pid, name, argv, ASSUAN_INVALID_FD,
		      ASSUAN_INVALID_FD, child_fds, at_socketpair_fork_cb,
		      &atp, 0);
  if (rc < 0)
    {
      err = gpg_err_code_from_syserror ();
      _assuan_close (ctx, fds[0]);
      _assuan_close (ctx, fds[1]);
      _assuan_free (ctx, child_fds);
      return TRACE_ERR (err);
    }

  /* For W32, the user needs to know the server-local names of the
     inherited handles.  Return them here.  Note that the translation
     of the peer socketpair fd (fd_child_list[0]) must be done by the
     wrapper program based on the environment variable
     _assuan_connection_fd.  */
  if (fd_child_list)
    {
      for (idx = 0; fd_child_list[idx] != -1; idx++)
	/* We add 1 to skip over the socketpair end.  */
	fd_child_list[idx] = child_fds[idx + 1];
    }

  _assuan_free (ctx, child_fds);

  /* If this is the server child process, exit early.  */
  if (! name && (*argv)[0] == 's')
    {
      _assuan_close (ctx, fds[0]);
      return 0;
    }

  _assuan_close (ctx, fds[1]);

  ctx->engine.release = _assuan_client_release;
  ctx->finish_handler = _assuan_client_finish;
  ctx->max_accepts = 1;
  ctx->inbound.fd  = fds[0];
  ctx->outbound.fd = fds[0];
  _assuan_init_uds_io (ctx);

  err = initial_handshake (ctx);
  if (err)
    _assuan_reset (ctx);
  return err;
}
Example #24
0
/**
 * Process a sentence with xml annotation
 * Xml tags may specifiy additional/replacing translation options
 * and reordering constraints
 *
 * \param line in: sentence, out: sentence without the xml
 * \param res vector with translation options specified by xml
 * \param reorderingConstraint reordering constraint zones specified by xml
 * \param walls reordering constraint walls specified by xml
 */
bool TreeInput::ProcessAndStripXMLTags(string &line, std::vector<XMLParseOutput> &sourceLabels, std::vector<XmlOption*> &xmlOptions)
{
  //parse XML markup in translation line

  // no xml tag? we're done.
  if (line.find_first_of('<') == string::npos) {
    return true;
  }

  // break up input into a vector of xml tags and text
  // example: (this), (<b>), (is a), (</b>), (test .)
  vector<string> xmlTokens = TokenizeXml(line);

  // we need to store opened tags, until they are closed
  // tags are stored as tripled (tagname, startpos, contents)
  typedef pair< string, pair< size_t, string > > OpenedTag;
  vector< OpenedTag > tagStack; // stack that contains active opened tags

  string cleanLine; // return string (text without xml)
  size_t wordPos = 0; // position in sentence (in terms of number of words)

  // keep this handy for later
  const vector<FactorType> &outputFactorOrder = StaticData::Instance().GetOutputFactorOrder();
  const string &factorDelimiter = StaticData::Instance().GetFactorDelimiter();

  // loop through the tokens
  for (size_t xmlTokenPos = 0 ; xmlTokenPos < xmlTokens.size() ; xmlTokenPos++) {
    // not a xml tag, but regular text (may contain many words)
    if(!isXmlTag(xmlTokens[xmlTokenPos])) {
      // add a space at boundary, if necessary
      if (cleanLine.size()>0 &&
          cleanLine[cleanLine.size() - 1] != ' ' &&
          xmlTokens[xmlTokenPos][0] != ' ') {
        cleanLine += " ";
      }
      cleanLine += xmlTokens[xmlTokenPos]; // add to output
      wordPos = Tokenize(cleanLine).size(); // count all the words
    }

    // process xml tag
    else {
      // *** get essential information about tag ***

      // strip extra boundary spaces and "<" and ">"
      string tag =  Trim(TrimXml(xmlTokens[xmlTokenPos]));
      VERBOSE(3,"XML TAG IS: " << tag << std::endl);

      if (tag.size() == 0) {
        TRACE_ERR("ERROR: empty tag name: " << line << endl);
        return false;
      }

      // check if unary (e.g., "<wall/>")
      bool isUnary = ( tag[tag.size() - 1] == '/' );

      // check if opening tag (e.g. "<a>", not "</a>")g
      bool isClosed = ( tag[0] == '/' );
      bool isOpen = !isClosed;

      if (isClosed && isUnary) {
        TRACE_ERR("ERROR: can't have both closed and unary tag <" << tag << ">: " << line << endl);
        return false;
      }

      if (isClosed)
        tag = tag.substr(1); // remove "/" at the beginning
      if (isUnary)
        tag = tag.substr(0,tag.size()-1); // remove "/" at the end

      // find the tag name and contents
      string::size_type endOfName = tag.find_first_of(' ');
      string tagName = tag;
      string tagContent = "";
      if (endOfName != string::npos) {
        tagName = tag.substr(0,endOfName);
        tagContent = tag.substr(endOfName+1);
      }

      // *** process new tag ***

      if (isOpen || isUnary) {
        // put the tag on the tag stack
        OpenedTag openedTag = make_pair( tagName, make_pair( wordPos, tagContent ) );
        tagStack.push_back( openedTag );
        VERBOSE(3,"XML TAG " << tagName << " (" << tagContent << ") added to stack, now size " << tagStack.size() << endl);
      }

      // *** process completed tag ***

      if (isClosed || isUnary) {
        // pop last opened tag from stack;
        if (tagStack.size() == 0) {
          TRACE_ERR("ERROR: tag " << tagName << " closed, but not opened" << ":" << line << endl);
          return false;
        }
        OpenedTag openedTag = tagStack.back();
        tagStack.pop_back();

        // tag names have to match
        if (openedTag.first != tagName) {
          TRACE_ERR("ERROR: tag " << openedTag.first << " closed by tag " << tagName << ": " << line << endl );
          return false;
        }

        // assemble remaining information about tag
        size_t startPos = openedTag.second.first;
        string tagContent = openedTag.second.second;
        size_t endPos = wordPos;

        // span attribute overwrites position
        string span = ParseXmlTagAttribute(tagContent,"span");
        if (! span.empty()) {
          vector<string> ij = Tokenize(span, "-");
          if (ij.size() != 1 && ij.size() != 2) {
            TRACE_ERR("ERROR: span attribute must be of the form \"i-j\" or \"i\": " << line << endl);
            return false;
          }
          startPos = atoi(ij[0].c_str());
          if (ij.size() == 1) endPos = startPos + 1;
          else endPos = atoi(ij[1].c_str()) + 1;
        }

        VERBOSE(3,"XML TAG " << tagName << " (" << tagContent << ") spanning " << startPos << " to " << (endPos-1) << " complete, commence processing" << endl);

        if (startPos >= endPos) {
          TRACE_ERR("ERROR: tag " << tagName << " must span at least one word: " << line << endl);
          return false;
        }

	// may be either a input span label ("label"), or a specified output translation "translation"
        string label = ParseXmlTagAttribute(tagContent,"label");
        string translation = ParseXmlTagAttribute(tagContent,"translation");

        // specified label
        if (translation.length() == 0 && label.length() > 0) {
          WordsRange range(startPos,endPos-1); // really?
          XMLParseOutput item(label, range);
          sourceLabels.push_back(item);
        }

        // specified translations -> vector of phrases, separated by "||"
        if (translation.length() > 0 && StaticData::Instance().GetXmlInputType() != XmlIgnore) {
          vector<string> altTexts = TokenizeMultiCharSeparator(translation, "||");
          vector<string> altLabel = TokenizeMultiCharSeparator(label, "||");
          vector<string> altProbs = TokenizeMultiCharSeparator(ParseXmlTagAttribute(tagContent,"prob"), "||");
	  //TRACE_ERR("number of translations: " << altTexts.size() << endl);
          for (size_t i=0; i<altTexts.size(); ++i) {
            // set target phrase
            TargetPhrase targetPhrase(Output);
            targetPhrase.CreateFromString(outputFactorOrder,altTexts[i],factorDelimiter);

            // set constituent label
	    string targetLHSstr;
            if (altLabel.size() > i && altLabel[i].size() > 0) {
              targetLHSstr = altLabel[i];
            }
            else {
              const UnknownLHSList &lhsList = StaticData::Instance().GetUnknownLHS();
              UnknownLHSList::const_iterator iterLHS = lhsList.begin();
              targetLHSstr = iterLHS->first;
            }
            Word targetLHS(true);
            targetLHS.CreateFromString(Output, outputFactorOrder, targetLHSstr, true);
            CHECK(targetLHS.GetFactor(0) != NULL);
            targetPhrase.SetTargetLHS(targetLHS);

            // get probability
            float probValue = 1;
            if (altProbs.size() > i && altProbs[i].size() > 0) {
              probValue = Scan<float>(altProbs[i]);
            }
            // convert from prob to log-prob
            float scoreValue = FloorScore(TransformScore(probValue));
            targetPhrase.SetScore(scoreValue);

            // set span and create XmlOption
            WordsRange range(startPos+1,endPos);
            XmlOption *option = new XmlOption(range,targetPhrase);
            CHECK(option);
            xmlOptions.push_back(option);

            VERBOSE(2,"xml translation = [" << range << "] " << targetLHSstr << " -> " << altTexts[i] << " prob: " << probValue << endl);
          }
          altTexts.clear();
          altProbs.clear();
        }
      }
    }
  }
  // we are done. check if there are tags that are still open
  if (tagStack.size() > 0) {
    TRACE_ERR("ERROR: some opened tags were never closed: " << line << endl);
    return false;
  }

  // return de-xml'ed sentence in line
  line = cleanLine;
  return true;
}
Example #25
0
void Data::loadnbest(const std::string &file)
{
  TRACE_ERR("loading nbest from " << file << std::endl);

  FeatureStats featentry;
  ScoreStats scoreentry;
  std::string sentence_index;

  inputfilestream inp(file); // matches a stream with a file. Opens the file

  if (!inp.good())
    throw runtime_error("Unable to open: " + file);

  std::string substring, subsubstring, stringBuf;
  std::string theSentence;
  std::string::size_type loc;

  while (getline(inp,stringBuf,'\n')) {
    if (stringBuf.empty()) continue;

//		TRACE_ERR("stringBuf: " << stringBuf << std::endl);

    getNextPound(stringBuf, substring, "|||"); //first field
    sentence_index = substring;

    getNextPound(stringBuf, substring, "|||"); //second field
    theSentence = substring;

// adding statistics for error measures
    featentry.reset();
    scoreentry.clear();

    theScorer->prepareStats(sentence_index, theSentence, scoreentry);

    scoredata->add(scoreentry, sentence_index);

    getNextPound(stringBuf, substring, "|||"); //third field

    // examine first line for name of features
    if (!existsFeatureNames()) {
      std::string stringsupport=substring;
      std::string features="";
      std::string tmpname="";

      size_t tmpidx=0;
      while (!stringsupport.empty()) {
        //			TRACE_ERR("Decompounding: " << substring << std::endl);
        getNextPound(stringsupport, subsubstring);

        // string ending with ":" are skipped, because they are the names of the features
        if ((loc = subsubstring.find_last_of(":")) != subsubstring.length()-1) {
          features+=tmpname+"_"+stringify(tmpidx)+" ";
          tmpidx++;
        }
        // ignore sparse feature name
        else if (subsubstring.find("_") != string::npos) {
          // also ignore its value
          getNextPound(stringsupport, subsubstring);
        }
        // update current feature name
        else {
          tmpidx=0;
          tmpname=subsubstring.substr(0,subsubstring.size() - 1);
        }
      }

      featdata->setFeatureMap(features);
    }

    // adding features
    while (!substring.empty()) {
//			TRACE_ERR("Decompounding: " << substring << std::endl);
      getNextPound(substring, subsubstring);

      // no ':' -> feature value that needs to be stored
      if ((loc = subsubstring.find_last_of(":")) != subsubstring.length()-1) {
        featentry.add(ATOFST(subsubstring.c_str()));
      }
      // sparse feature name? store as well
      else if (subsubstring.find("_") != string::npos) {
        std::string name = subsubstring;
        getNextPound(substring, subsubstring);
        featentry.addSparse( name, atof(subsubstring.c_str()) );
        _sparse_flag = true;
      }
    }
    //cerr << "number of sparse features: " << featentry.getSparse().size() << endl;
    featdata->add(featentry,sentence_index);
  }

  inp.close();
}
Example #26
0
static void aic_process_driver(struct event_entry *entry, struct queue_entry *q)
{
    struct aic_entry *e = (struct aic_entry*)entry;
    NEOERR *err;
    int ret;
    
    mdb_conn *db = e->db;
    struct cache *cd = e->cd;
    struct aic_stats *st = &(e->st);

    st->msg_total++;
    
    mtc_dbg("process cmd %u", q->operation);
    switch (q->operation) {
        CASE_SYS_CMD(q->operation, q, e->cd, err);
    case REQ_CMD_APPINFO:
        err = aic_cmd_appinfo(q, cd, db);
        break;
    case REQ_CMD_APPNEW:
        err = aic_cmd_appnew(q, cd, db);
        break;
    case REQ_CMD_APPUP:
        err = aic_cmd_appup(q, cd, db);
        break;
    case REQ_CMD_APPDEL:
        err = aic_cmd_appdel(q, cd, db);
        break;
    case REQ_CMD_APP_GETSECY:
        err = aic_cmd_app_getsecy(q, cd, db);
        break;
    case REQ_CMD_APP_SETSECY:
        err = aic_cmd_app_setsecy(q, cd, db);
        break;
    case REQ_CMD_APPUSERS:
        err = aic_cmd_appusers(q, cd, db);
        break;
    case REQ_CMD_APPUSERIN:
        err = aic_cmd_appuserin(q, cd, db);
        break;
    case REQ_CMD_APPUSEROUT:
        err = aic_cmd_appuserout(q, cd, db);
        break;
    case REQ_CMD_APPUSERUP:
        err = aic_cmd_appuserup(q, cd, db);
        break;
    case REQ_CMD_APP_O_USERS:
        err = aic_cmd_appousers(q, cd, db);
        break;
    case REQ_CMD_APP_GETRLINK:
        err = aic_cmd_app_getrlink(q, cd, db);
        break;
    case REQ_CMD_APP_SETRLINK:
        err = aic_cmd_app_setrlink(q, cd, db);
        break;
    case REQ_CMD_STATS:
        st->msg_stats++;
        err = STATUS_OK;
        hdf_set_int_value(q->hdfsnd, "msg_total", st->msg_total);
        hdf_set_int_value(q->hdfsnd, "msg_unrec", st->msg_unrec);
        hdf_set_int_value(q->hdfsnd, "msg_badparam", st->msg_badparam);
        hdf_set_int_value(q->hdfsnd, "msg_stats", st->msg_stats);
        hdf_set_int_value(q->hdfsnd, "proc_suc", st->proc_suc);
        hdf_set_int_value(q->hdfsnd, "proc_fai", st->proc_fai);
        break;
    default:
        st->msg_unrec++;
        err = nerr_raise(REP_ERR_UNKREQ, "unknown command %u", q->operation);
        break;
    }
    
    NEOERR *neede = mcs_err_valid(err);
    ret = neede ? neede->error : REP_OK;
    if (PROCESS_OK(ret)) {
        st->proc_suc++;
    } else {
        st->proc_fai++;
        if (ret == REP_ERR_BADPARAM) {
            st->msg_badparam++;
        }
        TRACE_ERR(q, ret, err);
    }
    if (q->req->flags & FLAGS_SYNC) {
        reply_trigger(q, ret);
    }
}
void PhraseDictionaryFuzzyMatch::InitializeForInput(InputType const& inputSentence)
{
#if defined __MINGW32__
  char dirName[] = "moses.XXXXXX";
#else
  char dirName[] = "/tmp/moses.XXXXXX";
#endif // defined
  char *temp = mkdtemp(dirName);
  UTIL_THROW_IF2(temp == NULL,
		  "Couldn't create temporary directory " << dirName);

  string dirNameStr(dirName);

  string inFileName(dirNameStr + "/in");

  ofstream inFile(inFileName.c_str());

  for (size_t i = 1; i < inputSentence.GetSize() - 1; ++i) {
    inFile << inputSentence.GetWord(i);
  }
  inFile << endl;
  inFile.close();

  long translationId = inputSentence.GetTranslationId();
  string ptFileName = m_FuzzyMatchWrapper->Extract(translationId, dirNameStr);

  // populate with rules for this sentence
  PhraseDictionaryNodeMemory &rootNode = m_collection[translationId];
  FormatType format = MosesFormat;

  // data from file
  InputFileStream inStream(ptFileName);

  // copied from class LoaderStandard
  PrintUserTime("Start loading fuzzy-match phrase model");

  const StaticData &staticData = StaticData::Instance();
  const std::string& factorDelimiter = staticData.GetFactorDelimiter();


  string lineOrig;
  size_t count = 0;

  while(getline(inStream, lineOrig)) {
    const string *line;
    if (format == HieroFormat) { // reformat line
      UTIL_THROW(util::Exception, "Cannot be Hiero format");
      //line = ReformatHieroRule(lineOrig);
    } else {
      // do nothing to format of line
      line = &lineOrig;
    }

    vector<string> tokens;
    vector<float> scoreVector;

    TokenizeMultiCharSeparator(tokens, *line , "|||" );

    if (tokens.size() != 4 && tokens.size() != 5) {
      UTIL_THROW2("Syntax error at " << ptFileName << ":" << count);
    }

    const string &sourcePhraseString = tokens[0]
                                       , &targetPhraseString = tokens[1]
                                           , &scoreString        = tokens[2]
                                               , &alignString        = tokens[3];

    bool isLHSEmpty = (sourcePhraseString.find_first_not_of(" \t", 0) == string::npos);
    if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
      TRACE_ERR( ptFileName << ":" << count << ": pt entry contains empty target, skipping\n");
      continue;
    }

    Tokenize<float>(scoreVector, scoreString);
    const size_t numScoreComponents = GetNumScoreComponents();
    if (scoreVector.size() != numScoreComponents) {
      UTIL_THROW2("Size of scoreVector != number (" << scoreVector.size() << "!="
            << numScoreComponents << ") of score components on line " << count);
    }

    UTIL_THROW_IF2(scoreVector.size() != numScoreComponents,
    		"Number of scores incorrectly specified");

    // parse source & find pt node

    // constituent labels
    Word *sourceLHS;
    Word *targetLHS;

    // source
    Phrase sourcePhrase( 0);
    // sourcePhrase.CreateFromString(Input, m_input, sourcePhraseString, factorDelimiter, &sourceLHS);
    sourcePhrase.CreateFromString(Input, m_input, sourcePhraseString, &sourceLHS);

    // create target phrase obj
    TargetPhrase *targetPhrase = new TargetPhrase(this);
    // targetPhrase->CreateFromString(Output, m_output, targetPhraseString, factorDelimiter, &targetLHS);
    targetPhrase->CreateFromString(Output, m_output, targetPhraseString, &targetLHS);

    // rest of target phrase
    targetPhrase->SetAlignmentInfo(alignString);
    targetPhrase->SetTargetLHS(targetLHS);
    //targetPhrase->SetDebugOutput(string("New Format pt ") + line);

    // component score, for n-best output
    std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),TransformScore);
    std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),FloorScore);

    targetPhrase->GetScoreBreakdown().Assign(this, scoreVector);
    targetPhrase->EvaluateInIsolation(sourcePhrase, GetFeaturesToApply());

    TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection(rootNode, sourcePhrase, *targetPhrase, sourceLHS);
    phraseColl.Add(targetPhrase);

    count++;

    if (format == HieroFormat) { // reformat line
      delete line;
    } else {
      // do nothing
    }

  }

  // sort and prune each target phrase collection
  SortAndPrune(rootNode);

  //removedirectoryrecursively(dirName);
}
// function request_card
static int KSAPI request_card(ks_reader_dev_t *dev,char* phyid,KS_CARD_TYPE t) {
    int ret;
    unsigned long snr;
    unsigned short tagtype;
    char tag_type[20] = {0};
    unsigned char size;
    uint8 ucCardPhyID[9] = {0};
    //uint8 rData[256];
    int phyidlen = 0;
    char ats[200] = {0};
    static char phyidtmp[8] = {0};
    if(!g_dev_hd) {
        ret=open_device(dev);
        if(ret)
            return ret;
    }
    if(!g_dev_hd) {
        return KS_PORT_NOTOPEN;
    }
    //if(t == KS_CPUCARD || t == KS_FIXCARD)
    if(dev->cpuport == CARDSLOT_RF) {
        //dc_reset(g_dev_hd,1);
        //ret = dc_request(g_dev_hd,1,&tagtype);
        //SAMCAR1_POWER_ON(g_dev_hd, ats, gErrMsg);
        //if (g_flag) {
        //	g_flag = 0;
        //	strcpy(phyid, (char*)phyidtmp);	//rw
        //	phyid[8]=0;
        //	return 0;
        //}
        MIFS_HALT(g_dev_hd, gErrMsg);
        ret = REQ_14443_CARD(g_dev_hd, 1 ,tag_type, (char*)ucCardPhyID, gErrMsg);
        //hex2dec((char*)ucCardPhyID, 8, (unsigned char*)phyid, phyidlen);
        if (ret) {
            RFSIM_HALT(g_dev_hd, gErrMsg);
            ret = REQ_RFSIM_CARD(g_dev_hd, 1, (char*)phyid, gErrMsg);
        }
        if(ret) {
            GetErrMsgByErrCode(ret,gErrMsg);
            TRACE_ERR("检测卡错误, "<<gErrMsg);
            return KS_REQUESTCARD;
        }
        //ret = ATS_14443_CARD(g_dev_hd, ats, gErrMsg);
        //if(ret)
        //{
        //	GetErrMsgByErrCode(ret,gErrMsg);
        //	TRACE_ERR("检测卡错误, "<<gErrMsg);
        //	return KS_REQUESTCARD;
        //}
        //ret = dc_select(g_dev_hd,snr,&size);
        //if(ret)
        //{
        //	GetErrMsgByErrCode(ret,gErrMsg);
        //	TRACE_ERR("检测卡错误, "<<gErrMsg);
        //	return KS_REQUESTCARD;
        //}
        tagtype = atoi(tag_type);
        switch(tagtype) {
#if 0
        case 400:
            g_lastCardType = KS_FIXCARD;
            break;
#else
        case 200:
        case 400:
            g_lastCardType = KS_MFCARD;
            break;
#endif
        case 4:
            if(size == 40)
                g_lastCardType = KS_FIXCARD;
            else
                g_lastCardType = KS_MFCARD; // S50
            break;
        case 2: // S70
            g_lastCardType = KS_MFCARD;
            break;
        default:
            g_lastCardType = KS_SIMCARD;
            break;
        }

        /*
        if(t == KS_CPUCARD || t == KS_FIXCARD)
        {
        ret = dc_pro_reset_hex(g_dev_hd, &size, (char *)rData);
        if(ret !=0 )
        {
        GetErrMsgByErrCode(ret,gErrMsg);
        TRACE_ERR("CPU卡复位错误, "<<gErrMsg);
        return KS_REQUESTCARD;
        }
        }
        */
        dev->cardtype = g_lastCardType;
        //set_4byte_int(ucCardPhyID,snr);
        //dec2hex(ucCardPhyID,4,phyid);
        if (g_lastCardType == KS_SIMCARD) {
            return 0;
        }
        char phyidtmp[8] = {0};
        phyidtmp[0] = ucCardPhyID[6];
        phyidtmp[1] = ucCardPhyID[7];
        phyidtmp[2] = ucCardPhyID[4];
        phyidtmp[3] = ucCardPhyID[5];
        phyidtmp[4] = ucCardPhyID[2];
        phyidtmp[5] = ucCardPhyID[3];
        phyidtmp[6] = ucCardPhyID[0];
        phyidtmp[7] = ucCardPhyID[1];
        memcpy((char*)ucCardPhyID, (char*)phyidtmp, 8);	//rw
        //ucCardPhyID[8]=0;
        strncpy(phyid, (char*)ucCardPhyID, 8);
        phyid[8] = 0;
    } else {
        TRACE_ERR("输入参数错误");
        return KS_REQUESTCARD;
    }
    return 0;
}
Example #29
0
/**
 * Creates listening socket to serve as a conduit between userland
 * applications and the system. Starts listening on all sockets 
 * thereafter.
 */
void
initiate_backend(engine *eng)
{
	TRACE_BACKEND_FUNC_START();
	struct epoll_event ev, events[EPOLL_MAX_EVENTS];
	int epoll_fd, nfds, n;
	uint i, dev_flag;

	dev_flag = 0;
	/* set up the epolling structure */
	epoll_fd = epoll_create(EPOLL_MAX_EVENTS);
	if (epoll_fd == -1) {
		TRACE_LOG("Engine %s failed to create an epoll fd!\n", 
			  eng->name);
		TRACE_BACKEND_FUNC_END();
		return;
	}

	/* create listening socket */
	create_listening_socket_for_eng(eng);

	/* register listening socket */
	ev.events = EPOLLIN | EPOLLOUT;
	ev.data.fd = eng->listen_fd;
	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, eng->listen_fd, &ev) == -1) {
		TRACE_LOG("Engine %s failed to exe epoll_ctl for fd: %d\n",
			  eng->name, epoll_fd);
		TRACE_BACKEND_FUNC_END();
		return;
	}
	
	TRACE_LOG("Engine %s is listening on port %d\n", 
		  eng->name, eng->listen_port);

	/* adjust pcapr context in engine */
	if (!strcmp(eng->FIRST_BRICK(esrc)->brick->elib->getId(), "PcapReader")) {
		eng->pcapr_context = eng->FIRST_BRICK(esrc)->brick->private_data;		
	}
	
	/* register iom socket */
	for (i = 0; i < eng->no_of_sources; i++) {
		ev.events = EPOLLIN;
		ev.data.fd = eng->esrc[i]->dev_fd;
		
		if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, eng->esrc[i]->dev_fd, &ev) == -1) {
			TRACE_LOG("Engine %s failed to exe epoll_ctl for fd: %d\n",
				  eng->name, epoll_fd);
			TRACE_BACKEND_FUNC_END();
			return;
		}
	}


	/* keep on running till engine stops */
	while (eng->run == 1) {
		nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT);
		if (nfds == -1) {
			TRACE_ERR("epoll error (engine: %s)\n",
				  eng->name);
			TRACE_BACKEND_FUNC_END();
		}
		for (n = 0; n < nfds; n++) {
			/* process dev work (check for all devs) */
			for (i = 0; i < eng->no_of_sources; i++) {
				if (events[n].data.fd == eng->esrc[i]->dev_fd) {
					eng->iom.callback(eng->esrc[i]);
					
					/* continue epolling */
					ev.data.fd = eng->esrc[i]->dev_fd;
					ev.events = EPOLLIN;
					if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, ev.data.fd, &ev) == -1) {
						TRACE_LOG("Engine %s failed to exe epoll_ctl for fd: %d\n",
							  eng->name, epoll_fd);
						TRACE_BACKEND_FUNC_END();
						return;
					}
					dev_flag = 1;
				} 
			}

			if (dev_flag == 1) {
				dev_flag = 0;
				continue;
			}
			/* process app reqs */
			if (events[n].data.fd == eng->listen_fd)
				process_request_backend(eng, epoll_fd);
			else {/* all other reqs coming from client socks */
				ev.data.fd = events[n].data.fd;
				if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, ev.data.fd, &ev) == -1) {
					TRACE_ERR("Can't delete client sock for epolling!\n");
					TRACE_BACKEND_FUNC_END();
				}	
				close(ev.data.fd);
			}
		}
		/* pcap handling.. */
		if (eng->pcapr_context != NULL)
			process_pcap_read_request(eng, eng->pcapr_context);
	}

	TRACE_BACKEND_FUNC_END();
}
Example #30
0
void mboot(void)
{
	U32 i;
	U32 ByteToRead;
	U32 ByteRead;

	FRESULT res;
	DIR dirs;
	FATFS fs;
	FIL FileObject;
	
	memset(&fs, 0, sizeof(FATFS));
	for(i=ZPARAMADDR; i < ZRELADDR; i++) *((volatile S8 *)(i)) = 0;
	
	res = f_mount(0, &fs);
	if( res != FR_OK )
	{
		TRACE_ERR("Mount OS SD card failed: 0x%X", res);
		return;
	}
	res = f_opendir(&dirs, STR_ROOT_DIRECTORY);
	if(res == FR_OK )
	{
		scan_files(STR_ROOT_DIRECTORY);
		
		TRACE_MSG("Load FPGA code (grid.bin) to buffer.");
		res = f_open(&FileObject, gridName, FA_OPEN_EXISTING|FA_READ);
		if(res != FR_OK)
		{
			TRACE_ERR("Open grid.rbf file failed: 0x%X", res);
			return;
		}		
		ByteToRead = FileObject.fsize;
		gridFileSize = FileObject.fsize;
		res = f_read(&FileObject, (void*)GRIDADDR, ByteToRead, &ByteRead);
		if(res != FR_OK)
		{
			TRACE_ERR("Load grid.rbf file failed: 0x%X", res);
			return;
		}
		else
		{
			TRACE_FIN("FPGA code file load OK.");
			FPGA_Config();
		}

		TRACE_MSG("Load kernel command line.");
		res = f_open(&FileObject, kcmdName, FA_OPEN_EXISTING|FA_READ);
		if(res != FR_OK)
		{
			TRACE_ERR("Open kernel command line file failed: 0x%X", res);
			return;
		}		
		ByteToRead = FileObject.fsize;
		kcmdFileSize = FileObject.fsize;
		res = f_read(&FileObject, (void*)(ZPARAMADDR + 4*11), ByteToRead, &ByteRead);

		if(res != FR_OK)
		{
			TRACE_ERR("Load kernel command line file failed: 0x%X", res);
			return;
		}
		
		TRACE_MSG("Load zImage to ZRELADDR: 0x%X.", ZRELADDR);
		res = f_open(&FileObject, zImageName, FA_OPEN_EXISTING|FA_READ);
		if(res != FR_OK)
		{
			TRACE_ERR("Open zImage file failed: 0x%X", res);
			return;
		}
		ByteToRead = FileObject.fsize;
		res = f_read(&FileObject, (void*)(ZRELADDR), ByteToRead, &ByteRead);
		if(res != FR_OK)
		{
			TRACE_ERR("Load zImage file failed: 0x%X", res);
			return;
		}
		else 
		{
			TRACE_FIN("zImage file Load OK, now let's just enjoy Linux :)\n\r");
			BTNDIS_N();
			Boot_Linux();
		}
	}
}