static void print_char(mps_t *mps, unsigned int prnr, const BYTE c) { if (mps->pos >= MAX_COL) { /* flush buffer*/ write_line(mps, prnr); clear_buffer(mps); } if (mps->tab) { /* decode tab-number*/ mps->tabc[2 - mps->tab] = c; if (mps->tab == 1) { mps->pos = is_mode(mps, MPS_ESC) ? mps->tabc[0] << 8 | mps->tabc[1] : atoi((char *)mps->tabc) * 6; del_mode(mps, MPS_ESC); } mps->tab--; return; } if (is_mode(mps, MPS_ESC) && (c != 16)) { del_mode(mps, MPS_ESC); } if (is_mode(mps, MPS_REPEAT)) { mps->repeatn = c; del_mode(mps, MPS_REPEAT); return; } if (is_mode(mps, MPS_BITMODE) && (c & 128)) { print_bitmask(mps, c); return; } /* it seems that CR works even in quote mode */ switch (c) { case 13: /* CR*/ mps->pos = 0; if (is_mode(mps, MPS_BUSINESS)) { del_mode(mps, MPS_CRSRUP); } else { set_mode(mps, MPS_CRSRUP); } /* CR resets Quote mode, revers mode, ... */ del_mode(mps, MPS_QUOTED); del_mode(mps, MPS_REVERSE); write_line(mps, prnr); clear_buffer(mps); return; } /* in text mode ignore most (?) other control chars when quote mode is active */ if (!is_mode(mps, MPS_QUOTED) || is_mode(mps, MPS_BITMODE)) { switch (c) { case 8: set_mode(mps, MPS_BITMODE); mps->bitcnt = 0; return; case 10: /* LF*/ write_line(mps, prnr); clear_buffer(mps); return; #ifdef notyet /* Not really sure if the MPS803 recognizes this one... */ case 13 + 128: /* shift CR: CR without LF (from 4023 printer) */ mps->pos = 0; if (is_mode(mps, MPS_BUSINESS)) { del_mode(mps, MPS_CRSRUP); } else { set_mode(mps, MPS_CRSRUP); } /* CR resets Quote mode, revers mode, ... */ del_mode(mps, MPS_QUOTED); del_mode(mps, MPS_REVERSE); return; #endif case 14: /* EN on*/ set_mode(mps, MPS_DBLWDTH); if (is_mode(mps, MPS_BITMODE)) { bitmode_off(mps); } return; case 15: /* EN off*/ del_mode(mps, MPS_DBLWDTH); if (is_mode(mps, MPS_BITMODE)) { bitmode_off(mps); } return; case 16: /* POS*/ mps->tab = 2; /* 2 chars (digits) following, number of first char*/ return; /* * By sending the cursor up code [CHR$(145)] to your printer, following * characters will be printed in cursor up (graphic) mode until either * a carriage return or cursor down code [CHR$(17)] is detected. * * By sending the cursor down code [CHR$(17)] to your printer, * following characters will be printed in business mode until either * a carriage return or cursor up code [CHR$(145)] is detected. */ case 17: /* crsr dn, enter businessmode local */ del_mode(mps, MPS_CRSRUP); return; case 145: /* CRSR up, enter gfxmode local */ set_mode(mps, MPS_CRSRUP); return; case 18: set_mode(mps, MPS_REVERSE); return; case 146: /* 18+128*/ del_mode(mps, MPS_REVERSE); return; case 26: /* repeat last chr$(8) c times.*/ set_mode(mps, MPS_REPEAT); mps->repeatn = 0; mps->bitcnt = 0; return; case 27: set_mode(mps, MPS_ESC); /* followed by 16, and number MSB, LSB*/ return; } } if (is_mode(mps, MPS_BITMODE)) { return; } /* * When an odd number of CHR$(34) is detected in a line, the control * codes $00-$1F and $80-$9F will be made visible by printing a * reverse character for each of these controls. This will continue * until an even number of quotes [CHR$(34)] has been received or until * end of this line. */ if (c == 34) { mps->mode ^= MPS_QUOTED; } if (is_mode(mps, MPS_QUOTED)) { if (c <= 0x1f) { set_mode(mps, MPS_REVERSE); print_cbm_char(mps, (BYTE)(c + 0x40)); del_mode(mps, MPS_REVERSE); return; } if ((c >= 0x80) && (c <= 0x9f)) { set_mode(mps, MPS_REVERSE); print_cbm_char(mps, (BYTE)(c - 0x20)); del_mode(mps, MPS_REVERSE); return; } } print_cbm_char(mps, c); }
int main(int argc, char *argv[]) { char opt, *framename, *listname, *outname, *e_outname, buffer[BUFSIZ]; int n, row, col, cX, cY, npatch, npxi, npxt, nprof; int flag_autoname, flag_verbose, status, bitmask[YAR][XAR]; long xsize, ysize, lowleft[DIM], upright[DIM], increment[DIM] = {1L, 1L}, framesize[DIM]; float mask_val, cutoff, framefragment[YAR][XAR]; struct star* patchstars; FILE *listfile; fitsfile *frame, *maskedframe; extern char *optarg; extern int opterr; framename = NULL; listname = NULL; outname = NULL; mask_val = DEFAULT_MASK; cutoff = DEFAULT_CUTOFF; flag_autoname = 1; flag_verbose = 0; opterr = 0; while ((opt=getopt(argc, argv, "f:l:o:m:c:v")) != -1) { switch (opt) { case 'f': framename = optarg; break; case 'l': listname = optarg; break; case 'o': flag_autoname = 0; outname = optarg; break; case 'm': mask_val = atof(optarg); break; case 'c': cutoff = atof(optarg); break; case 'v': flag_verbose = 1; break; default: usage(argv); } } /* check mandatory parameters: */ if (framename == NULL || listname == NULL) usage(argv); if (cutoff < 0.0) { fprintf(stderr, " WARNING: invalid cut-off value (%.1f ADU), using default (%.1f ADU)\n", cutoff, DEFAULT_CUTOFF); cutoff = DEFAULT_CUTOFF; } /* read ID and coordinates from any single-lined Daophot output, detect and skip the header if necessary: */ if ((listfile=fopen(listname, "r")) == NULL) { file_read_error(listname); } else { npatch = line_count(listfile); if (has_daophot_header(listfile) == 1) { npatch -= DAO_HEADER_SIZE; line_skip(listfile, DAO_HEADER_SIZE); } if (npatch <= 0) { fclose(listfile); fprintf(stderr, " couldn't read contents of '%s', exiting.\n\n", listname); exit(EXIT_FAILURE); } patchstars = calloc(npatch, sizeof(struct star)); if (patchstars == NULL) { fclose(listfile); memory_error(); } for (n=0; n < npatch; n++) { fgets(buffer, BUFSIZ, listfile); sscanf(buffer, "%d %lf %lf", &patchstars[n].num, &patchstars[n].X, &patchstars[n].Y); } fclose(listfile); } status = 0; fits_open_file(&frame, framename, READONLY, &status); if (status != 0) { free(patchstars); file_read_error(framename); } fits_get_img_size(frame, DIM, framesize, &status); fits_print_error(status); xsize = framesize[0]; ysize = framesize[1]; printf(" %s: %ld x %ld pixels\n", framename, xsize, ysize); printf(" %s: %d stars in the list\n", listname, npatch); printf(" mask value: %.1f ADU\n", mask_val); printf(" cut-off level: %.1f ADU\n", cutoff); if (flag_autoname == 1) { outname = expand_filename(framename, "-msk", 0, 0); if (outname == NULL) { free(patchstars); fits_close_file(frame, &status); memory_error(); } } printf(" output file: %s\n", outname); /* force cfitsio to overwrite already existing file (prepend ! to the filename) */ e_outname = prepend_bang(outname); if (e_outname == NULL) { free(patchstars); fits_close_file(frame, &status); memory_error(); } /* create output (masked) frame: */ fits_create_file(&maskedframe, e_outname, &status); fits_print_error(status); free(e_outname); if (flag_autoname == 1) /* only necessary if outname was created automatically */ free(outname); fits_copy_file(frame, maskedframe, 1, 1, 1, &status); fits_print_error(status); npxt = 0; nprof = 0; /* process the stars in the list: */ for (n=0; n < npatch; n++) { if (flag_verbose == 1) printf("\n star #%d:\n", patchstars[n].num); /* cfitsio doesn't understand fractional pixels, round the coordinates to nearest integer: */ cX = rint(patchstars[n].X); cY = rint(patchstars[n].Y); /* make sure center is in the picture, skip this star if it is not: */ if (cX < 1 || cY < 1 || cX > xsize || cY > ysize) { printf(" star #%d is outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY); continue; } /* * in fitsio pixel index runs from 1 to N, not from 0 to N-1. * consider XAR=21 (reading 10 pixels right and left from center), XSIZE=2044. * example 1: star with x=10. fitsio will reach and try to read pixel at x=0, ILLEGAL. * example 2: star with x=2034. fitsio will reach and try to read pixel at x=2044, LEGAL. * we have to use LESS OR EQUAL when checking lower and left boundary, * for upper and right boundary only using GREATER is fine */ else if (cX <= (XAR-1)/2 || cY <= (YAR-1)/2 || cX > xsize-(XAR-1)/2 || cY > ysize-(YAR-1)/2) { printf(" star #%d is too close to the edge or partially outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY); continue; } if (flag_verbose == 1) printf(" coordinates of star #%d center in current frame: (%d,%d)\n", patchstars[n].num, cX, cY); /* compute the coordinates of lower left and upper right pixel: */ lowleft[0] = cX - (XAR-1)/2; lowleft[1] = cY - (YAR-1)/2; upright[0] = cX + (XAR-1)/2; upright[1] = cY + (YAR-1)/2; /* load frame fragment with the star into table: */ fits_read_subset(frame, TFLOAT, lowleft, upright, increment, 0, framefragment, 0, &status); fits_print_error(status); /* initialise the bitmask with zeros: */ for (row=0; row < YAR; row++) for (col=0; col < XAR; col++) bitmask[row][col] = 0; /* detect holes and write to bitmask: */ for (row=YAR-1; row >= 0; row--) examine_row(framefragment, bitmask, row, cutoff); for (col=0; col < XAR; col++) examine_col(framefragment, bitmask, col, cutoff); npxi = apply_bitmask(framefragment, bitmask, mask_val); npxt += npxi; if (npxi > 0) ++nprof; if (flag_verbose == 1) { printf("\n"); print_bitmask(bitmask); printf("\n star %d: %d pixels masked\n", patchstars[n].num, npxi); } /* write the modified frame subsection to output frame: */ fits_write_subset(maskedframe, TFLOAT, lowleft, upright, framefragment, &status); fits_print_error(status); } printf(" %d pixels were masked in %d stars.\n", npxt, nprof); fits_close_file(frame, &status); fits_print_error(status); fits_close_file(maskedframe, &status); fits_print_error(status); free(patchstars); return 0; }
static void print_char(mps_t *mps, unsigned int prnr, const BYTE c) { if (mps->pos >= MAX_COL) { /* flush buffer*/ write_line(mps, prnr); clear_buffer(mps); } if (mps->tab) { /* decode tab-number*/ mps->tabc[2 - mps->tab] = c; if (mps->tab == 1) { mps->pos = is_mode(mps, MPS_ESC) ? mps->tabc[0] << 8 | mps->tabc[1] : atoi((char *)mps->tabc) * 6; del_mode(mps, MPS_ESC); } mps->tab--; return; } if (is_mode(mps, MPS_ESC) && c != 16) { del_mode(mps, MPS_ESC); } if (is_mode(mps, MPS_REPEAT)) { mps->repeatn = c; del_mode(mps, MPS_REPEAT); return; } if (is_mode(mps, MPS_BITMODE) && c & 128) { print_bitmask(mps, c); return; } switch (c) { case 8: set_mode(mps, MPS_BITMODE); mps->bitcnt = 0; return; case 10: /* LF*/ write_line(mps, prnr); clear_buffer(mps); return; case 13: /* CR*/ mps->pos = 0; del_mode(mps, MPS_CRSRUP); write_line(mps, prnr); clear_buffer(mps); return; /* * By sending the cursor up code [CHR$(145)] to your printer, folowing * characters will be printed in cursor up (graphic) mode until either * a carriage return or cursor down code [CHR$(17)] is detected. * * By sending the cursor down code [CHR$(145)] to your printer, * following characters will be printed in business mode until either * a carriage return or cursor up code [CHR$(145)] is detected. * * 1. GRAPHIC MODE Code & Front Table, OMITTED * When an old number of CHR$(34) is detected in a line, the control * codes $00-$1F and $80-$9F will be made visible by printing a * reverse character for each of these controls. This will continue * until an even number of quotes [CHR$(34)] has been received or until * end of this line. * * 2. BUSINESS MODE Code & Font Table, OMITTED * When an old number of CHR$(34) is detected in a line, the control * codes $00-$1F and $80-$9F will be made visible by printing a * reverse character for each of these controls. This will continue * until an even number of quotes [CHR$(34)] has been received or until * end of this line. */ case 14: /* EN on*/ set_mode(mps, MPS_DBLWDTH); if (is_mode(mps, MPS_BITMODE)) { bitmode_off(mps); } return; case 15: /* EN off*/ del_mode(mps, MPS_DBLWDTH); if (is_mode(mps, MPS_BITMODE)) { bitmode_off(mps); } return; case 16: /* POS*/ mps->tab = 2; /* 2 chars (digits) following, number of first char*/ return; case 17: /* crsr dn*/ del_mode(mps, MPS_CRSRUP); return; case 18: set_mode(mps, MPS_REVERSE); return; case 26: /* repeat last chr$(8) c times.*/ set_mode(mps, MPS_REPEAT); mps->repeatn = 0; mps->bitcnt = 0; return; case 27: set_mode(mps, MPS_ESC); /* followed by 16, and number MSB, LSB*/ return; case 145: /* CRSR up*/ set_mode(mps, MPS_CRSRUP); return; case 146: /* 18+128*/ del_mode(mps, MPS_REVERSE); return; } if (is_mode(mps, MPS_BITMODE)) { return; } print_cbm_char(mps, c); }
int main(int argc, const char **argv) { int num_cpus = numa_num_task_cpus(); printf("num cpus: %d\n", num_cpus); printf("numa available: %d\n", numa_available()); numa_set_localalloc(); struct bitmask *bm = numa_bitmask_alloc(num_cpus); for (int i=0; i<=numa_max_node(); ++i) { numa_node_to_cpus(i, bm); printf("numa node %d ", i); print_bitmask(bm); printf(" - %g GiB\n", numa_node_size(i, 0) / (1024.*1024*1024.)); } numa_bitmask_free(bm); puts(""); char *x; const size_t cache_line_size = 64; const size_t array_size = 100*1000*1000; size_t ntrips = 2; #pragma omp parallel { assert(omp_get_num_threads() == num_cpus); int tid = omp_get_thread_num(); pin_to_core(tid); if(tid == 0) x = (char *) numa_alloc_local(array_size); // {{{ single access #pragma omp barrier for (size_t i = 0; i<num_cpus; ++i) { if (tid == i) { double t = measure_access(x, array_size, ntrips); printf("sequential core %d -> core 0 : BW %g MB/s\n", i, array_size*ntrips*cache_line_size / t / 1e6); } #pragma omp barrier } // }}} // {{{ everybody contends for one { if (tid == 0) puts(""); #pragma omp barrier double t = measure_access(x, array_size, ntrips); #pragma omp barrier for (size_t i = 0; i<num_cpus; ++i) { if (tid == i) printf("all-contention core %d -> core 0 : BW %g MB/s\n", tid, array_size*ntrips*cache_line_size / t / 1e6); #pragma omp barrier } } // }}} // {{{ zero and someone else contending if (tid == 0) puts(""); #pragma omp barrier for (size_t i = 1; i<num_cpus; ++i) { double t; if (tid == i || tid == 0) t = measure_access(x, array_size, ntrips); #pragma omp barrier if (tid == 0) { printf("two-contention core %d -> core 0 : BW %g MB/s\n", tid, array_size*ntrips*cache_line_size / t / 1e6); } #pragma omp barrier if (tid == i) { printf("two-contention core %d -> core 0 : BW %g MB/s\n\n", tid, array_size*ntrips*cache_line_size / t / 1e6); } #pragma omp barrier } } numa_free(x, array_size); return 0; }