Пример #1
0
int
main(int argc, char *argv[])
{
	struct info i;
	enum FMT fmt;
	int ch;

	setprogname(argv[0]);
	(void)setlocale(LC_ALL, "");

	fmt = STTY_NOTSET;
	i.fd = STDIN_FILENO;

	opterr = 0;
	while (optind < argc &&
	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
	    (ch = getopt(argc, argv, "aef:g")) != -1)
		switch(ch) {
		case 'a':		/* undocumented: POSIX compatibility */
			fmt = STTY_POSIX;
			break;
		case 'e':
			fmt = STTY_BSD;
			break;
		case 'f':
			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
				err(1, "%s", optarg);
			break;
		case 'g':
			fmt = STTY_GFLAG;
			break;
		case '?':
		default:
			goto args;
		}

args:	argc -= optind;
	argv += optind;

	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
		err(1, "TIOCGETD");
	if (tcgetattr(i.fd, &i.t) < 0)
		err(1, "tcgetattr");
	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
		warn("TIOCGWINSZ");

	switch(fmt) {
	case STTY_NOTSET:
		if (*argv)
			break;
		/* FALLTHROUGH */
	case STTY_BSD:
	case STTY_POSIX:
		print(&i.t, &i.win, i.ldisc, fmt);
		break;
	case STTY_GFLAG:
		gprint(&i.t);
		break;
	}

	for (i.set = i.wset = 0; *argv; ++argv) {
		if (ksearch(&argv, &i))
			continue;

		if (csearch(&argv, &i))
			continue;

		if (msearch(&argv, &i))
			continue;

		if (isdigit((unsigned char)**argv)) {
			int speed;

			speed = atoi(*argv);
			cfsetospeed(&i.t, speed);
			cfsetispeed(&i.t, speed);
			i.set = 1;
			continue;
		}

		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
			gread(&i.t, *argv + sizeof("gfmt1") - 1);
			i.set = 1;
			continue;
		}

		warnx("illegal option -- %s", *argv);
		usage();
	}

	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
		err(1, "tcsetattr");
	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
		warn("TIOCSWINSZ");
	exit(0);
	/* NOTREACHED */
}
Пример #2
0
void guppi_rawdisk_thread(void *_args) {

    /* Set cpu affinity */
    cpu_set_t cpuset, cpuset_orig;
    sched_getaffinity(0, sizeof(cpu_set_t), &cpuset_orig);
    CPU_ZERO(&cpuset);
    CPU_SET(1, &cpuset);
    int rv = sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
    if (rv<0) { 
        guppi_error("guppi_rawdisk_thread", "Error setting cpu affinity.");
        perror("sched_setaffinity");
    }

    /* Get args */
    struct guppi_thread_args *args = (struct guppi_thread_args *)_args;

    /* Set priority */
    rv = setpriority(PRIO_PROCESS, 0, 0);
    if (rv<0) {
        guppi_error("guppi_rawdisk_thread", "Error setting priority level.");
        perror("set_priority");
    }

    /* Attach to status shared mem area */
    struct guppi_status st;
    rv = guppi_status_attach(&st);
    if (rv!=GUPPI_OK) {
        guppi_error("guppi_rawdisk_thread", 
                "Error attaching to status shared memory.");
        pthread_exit(NULL);
    }
    pthread_cleanup_push((void *)guppi_status_detach, &st);
    pthread_cleanup_push((void *)set_exit_status, &st);

    /* Init status */
    guppi_status_lock_safe(&st);
    hputs(st.buf, STATUS_KEY, "init");
    guppi_status_unlock_safe(&st);

    /* Read in general parameters */
    struct guppi_params gp;
#if FITS_TYPE == PSRFITS
    struct sdfits pf;
    pf.sub.dat_freqs = NULL;
    pf.sub.dat_weights = NULL;
    pf.sub.dat_offsets = NULL;
    pf.sub.dat_scales = NULL;
    pthread_cleanup_push((void *)guppi_free_psrfits, &pf);
#else
    struct sdfits pf;
    pthread_cleanup_push((void *)guppi_free_sdfits, &pf);
#endif

    /* Attach to databuf shared mem */
    struct guppi_databuf *db;
    db = guppi_databuf_attach(args->input_buffer);
    if (db==NULL) {
        guppi_error("guppi_rawdisk_thread",
                "Error attaching to databuf shared memory.");
        pthread_exit(NULL);
    }
    pthread_cleanup_push((void *)guppi_databuf_detach, db);

    /* Init output file */
    FILE *fraw = NULL;
    pthread_cleanup_push((void *)safe_fclose, fraw);

    /* Pointers for quantization params */
    double *mean = NULL;
    double *std = NULL;
    printf("casper: raw disk thread created and running\n");
    /* Loop */
    int packetidx=0, npacket=0, ndrop=0, packetsize=0, blocksize=0;
    int orig_blocksize=0;
    int curblock=0;
    int block_count=0, blocks_per_file=128, filenum=0;
    int got_packet_0=0, first=1;
    int requantize = 0;
    char *ptr, *hend;
    signal(SIGINT,cc);
    while (run_threads) {

        /* Note waiting status */
        guppi_status_lock_safe(&st);
        hputs(st.buf, STATUS_KEY, "waiting");
        guppi_status_unlock_safe(&st);

        /* Wait for buf to have data */
        rv = guppi_databuf_wait_filled(db, curblock);
        if (rv!=0) continue;

	    printf("casper: raw disk thread rcvd data.\n");

        /* Read param struct for this block */
        ptr = guppi_databuf_header(db, curblock);
        if (first) {
            guppi_read_obs_params(ptr, &gp, &pf);
            first = 0;
        } else {
            guppi_read_subint_params(ptr, &gp, &pf);
        }

        /* Parse packet size, npacket from header */
        hgeti4(ptr, "PKTIDX", &packetidx);
        hgeti4(ptr, "PKTSIZE", &packetsize);
        hgeti4(ptr, "NPKT", &npacket);
        hgeti4(ptr, "NDROP", &ndrop);

#if FITS_TYPE == PSR_FITS
        /* Check for re-quantization flag */
        int nbits_req = 0;
        if (hgeti4(ptr, "NBITSREQ", &nbits_req) == 0) {
            /* Param not present, don't requantize */
            requantize = 0;
        } else {
            /* Param is present */
            if (nbits_req==8)
                requantize = 0;
            else if (nbits_req==2) 
                requantize = 1;
            else
                /* Invalid selection for requested nbits 
                 * .. die or ignore?
                 */
                requantize = 0;
        }
#endif

        /* Set up data ptr for quant routines */
#if FITS_TYPE == PSR_FITS
        pf.sub.data = (unsigned char *)guppi_databuf_data(db, curblock);
#else
        pf.data_columns.data = (unsigned char *)guppi_databuf_data(db, curblock);
#endif

        /* Wait for packet 0 before starting write */
        if (got_packet_0==0 && packetidx==0 && gp.stt_valid==1) {
            got_packet_0 = 1;
            guppi_read_obs_params(ptr, &gp, &pf);
#if FITS_TYPE == PSR_FITS
            orig_blocksize = pf.sub.bytes_per_subint;
#endif
            char fname[256];
            sprintf(fname, "%s.%4.4d.raw", pf.basefilename, filenum);
            fprintf(stderr, "Opening raw file '%s'\n", fname);
            // TODO: check for file exist.
            fraw = fopen(fname, "w");
            if (fraw==NULL) {
                guppi_error("guppi_rawdisk_thread", "Error opening file.");
                pthread_exit(NULL);
            }

#if FITS_TYPE == PSR_FITS
            /* Determine scaling factors for quantization if appropriate */
            if (requantize) {
                mean = (double *)realloc(mean, 
                        pf.hdr.rcvr_polns * pf.hdr.nchan * sizeof(double));
                std  = (double *)realloc(std,  
                        pf.hdr.rcvr_polns * pf.hdr.nchan * sizeof(double));
                compute_stat(&pf, mean, std);
                fprintf(stderr, "Computed 2-bit stats\n");
            }
#endif
        }
        
        /* See if we need to open next file */
        if (block_count >= blocks_per_file) {
            fclose(fraw);
            filenum++;
            char fname[256];
            sprintf(fname, "%s.%4.4d.raw", pf.basefilename, filenum);
            fprintf(stderr, "Opening raw file '%s'\n", fname);
            fraw = fopen(fname, "w");
            if (fraw==NULL) {
                guppi_error("guppi_rawdisk_thread", "Error opening file.");
                pthread_exit(NULL);
            }
            block_count=0;
        }

        /* See how full databuf is */
        //total_status = guppi_databuf_total_status(db);

        /* Requantize from 8 bits to 2 bits if necessary.
         * See raw_quant.c for more usage examples.
         */
#if FITS_TYPE == PSR_FITS
        if (requantize && got_packet_0) {
            pf.sub.bytes_per_subint = orig_blocksize;
            /* Does the quantization in-place */
            quantize_2bit(&pf, mean, std);
            /* Update some parameters for output */
            hputi4(ptr, "BLOCSIZE", pf.sub.bytes_per_subint);
            hputi4(ptr, "NBITS", pf.hdr.nbits);
        }
#endif

        /* Get full data block size */
        hgeti4(ptr, "BLOCSIZE", &blocksize);

        /* If we got packet 0, write data to disk */
        if (got_packet_0) { 

            /* Note waiting status */
            guppi_status_lock_safe(&st);
            hputs(st.buf, STATUS_KEY, "writing");
            guppi_status_unlock_safe(&st);

            /* Write header to file */
            hend = ksearch(ptr, "END");
            for (ptr=ptr; ptr<=hend; ptr+=80) {
                fwrite(ptr, 80, 1, fraw);
            }

            /* Write data */
            printf("block size: %d\n", blocksize);
            ptr = guppi_databuf_data(db, curblock);
            rv = fwrite(ptr, 1, (size_t)blocksize, fraw);
            if (rv != blocksize) { 
                guppi_error("guppi_rawdisk_thread", 
                        "Error writing data.");
            }

            /* Increment counter */
            block_count++;

            /* flush output */
            fflush(fraw);
        }

        /* Mark as free */
        guppi_databuf_set_free(db, curblock);

        /* Go to next block */
        curblock = (curblock + 1) % db->n_block;

        /* Check for cancel */
        pthread_testcancel();

    }

    pthread_exit(NULL);

    pthread_cleanup_pop(0); /* Closes fclose */
    pthread_cleanup_pop(0); /* Closes guppi_databuf_detach */
    pthread_cleanup_pop(0); /* Closes guppi_free_psrfits */
    pthread_cleanup_pop(0); /* Closes set_exit_status */
    pthread_cleanup_pop(0); /* Closes guppi_status_detach */

}
Пример #3
0
int
main(int argc, char *argv[])
{
	struct info i;
	enum FMT fmt;
	int ch;

	fmt = NOTSET;
	i.fd = STDIN_FILENO;

	opterr = 0;
	while (optind < argc &&
	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
	    (ch = getopt(argc, argv, "aef:g")) != -1)
		switch(ch) {
		case 'a':		/* undocumented: POSIX compatibility */
			fmt = POSIX;
			break;
		case 'e':
			fmt = BSD;
			break;
		case 'f':
			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
				err(1, "%s", optarg);
			break;
		case 'g':
			fmt = GFLAG;
			break;
		default:
			goto args;
		}

args:	argc -= optind;
	argv += optind;

	if (tcgetattr(i.fd, &i.t) < 0)
		errx(1, "not a terminal");
	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0	)
		err(1, "TIOCGETD");
	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
		warn("TIOCGWINSZ");

	switch(fmt) {
	case NOTSET:
		if (*argv)
			break;
		/* FALLTHROUGH */
	case BSD:
	case POSIX:
		print(&i.t, &i.win, i.ldisc, fmt);
		break;
	case GFLAG:
		gprint(&i.t, &i.win, i.ldisc);
		break;
	}

	for (i.set = i.wset = 0; *argv; ++argv) {
		if (ksearch(&argv, &i))
			continue;

		if (csearch(&argv, &i))
			continue;

		if (msearch(&argv, &i))
			continue;

		if (isdigit(**argv)) {
			const char *error;
			int speed;

			speed = strtonum(*argv, 0, INT_MAX, &error);
			if (error)
				err(1, "%s", *argv);
			cfsetospeed(&i.t, speed);
			cfsetispeed(&i.t, speed);
			i.set = 1;
			continue;
		}

		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
			gread(&i.t, *argv + sizeof("gfmt1") - 1);
			i.set = 1;
			continue;
		}

		warnx("illegal option -- %s", *argv);
		usage();
	}

	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
		err(1, "tcsetattr");
	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
		warn("TIOCSWINSZ");
	return (0);
}
Пример #4
0
	void operator()(std::string& readStr,
		std::vector<QuasiAlignment>& hits,
		MateStatus mateStatus) {

	    auto jfhash = rmi_->merHash.get();
	    auto& kmerInfos = rmi_->kmerInfos;
	    auto& eqClasses = rmi_->eqClassList;
	    auto& eqClassLabels = rmi_->eqLabelList;
	    auto& posList = rmi_->posList;
	    auto& fwdJumpTable = rmi_->fwdJumpTable;
	    auto& revJumpTable = rmi_->revJumpTable;
	    auto posEnd = posList.end();

	    auto k = rapmap::utils::my_mer::k();
	    auto readLen = readStr.length();
	    uint32_t maxDist = static_cast<uint32_t>(readLen) * 1.5;

	    auto endIt = kmerInfos.end();

	    std::vector<HitInfo> kmerHits;
	    uint64_t merID;
	    size_t kID;
	    rapmap::utils::my_mer searchBuffer;

	    bool terminateSearch{false}; // terminate search after checking the *next* hit
	    bool validKmer{false};
	    uint32_t numAnchors{0};
	    uint32_t searchPos{0};
	    uint32_t jumpLen{0};
	    bool isRC;
	    SkippingKmerSearcher ksearch(readStr);

	    while (ksearch.isValid()) {
		auto searchMer = ksearch.getMer(isRC);
		bool foundMer = jfhash->get_val_for_key(searchMer, &merID,
			searchBuffer, &kID);
		// OK --- we found a hit
		if (foundMer) {

		    kmerHits.emplace_back(kmerInfos.begin() + merID,
			    merID, ksearch.queryIndex(), !isRC);
		    // Increment the # of anchor k-mers we found
		    ++numAnchors;

		    // If we decided to terminate the search in the last loop, then we're done.
		    if (terminateSearch) { break; }

		    jumpLen = isRC ? revJumpTable[merID] :
			      fwdJumpTable[merID];
		    //js.jumpSizes += jumpLen;
		    //++js.numJumps;

		    if (jumpLen > 1) { // only jump if it's worth it
			if (ksearch.isOutsideQuery(jumpLen)) {
			    if (ksearch.backwardSearch(searchPos)) {
				terminateSearch = true;
				continue;
			    } else {
				break;
			    }
			}

			std::tie(validKmer, searchPos) = ksearch.skipForward(jumpLen);
			if (!validKmer) {
			    // There was no valid k-mer from the skip position to the end
			    // of the read --- execute reverse search

			    // But -- only do so if we don't have at least 2 anchors
			    if (numAnchors >= 2) { break; }

			    if (ksearch.backwardSearch(searchPos)) {
				// If we find something in the reverse search, it will
				// be the last thing we check.
				terminateSearch = true;
				continue;
			    } else {
				// Otherwise, if we don't find anything in the reverse
				// search --- just give up and take what we have.
				break;
			    }
			} else {
			    if (searchPos == readLen - k) {
				terminateSearch = true;
			    }
			    // The skip was successful --- continue the search normally
			    // from here.
			    continue;
			}
		    }
		} else {
		    if (terminateSearch) { break; }
		}
		ksearch.next();
	    }

	    // found no hits in the entire read
	    if (kmerHits.size() == 0) { return; }

	    if (kmerHits.size() > 0) {
		if (kmerHits.size() > 1) {
		    //std::cerr << "kmerHits.size() = " << kmerHits.size() << "\n";
		    auto processedHits = rapmap::hit_manager::intersectHits(kmerHits, *rmi_);
		    rapmap::hit_manager::collectHitsSimple(processedHits, readLen, maxDist, hits, mateStatus);
		} else {
		    // std::cerr << "kmerHits.size() = " << kmerHits.size() << "\n";
		    auto& kinfo = *kmerHits[0].kinfo;
		    hits.reserve(kinfo.count);
		    // Iterator into, length of and end of the transcript list
		    auto& eqClassLeft = eqClasses[kinfo.eqId];
		    // Iterator into, length of and end of the positon list
		    auto leftPosIt = posList.begin() + kinfo.offset;
		    auto leftPosLen = kinfo.count;
		    auto leftPosEnd = leftPosIt + leftPosLen;
		    PositionListHelper leftPosHelper(leftPosIt, posList.end());
		    bool leftHitRC = kmerHits[0].queryRC;

		    auto leftTxpIt = eqClassLabels.begin() + eqClassLeft.txpListStart;
		    auto leftTxpListLen = eqClassLeft.txpListLen;
		    auto leftTxpEnd = leftTxpIt + leftTxpListLen;

		    for (auto it = leftTxpIt; it < leftTxpEnd; ++it) {
			collectAllHits(*it, readLen, leftHitRC, leftPosHelper, hits, mateStatus);
		    }
		}
	    }

	}