IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 36057


Ignore:
Timestamp:
Aug 30, 2013, 1:00:26 PM (13 years ago)
Author:
bills
Message:

If request specifies the tess_id set up a "skycell server" process and
and send lookups to it. This speeds up parsing of bycoord requests by about
a factor of three by avoiding repeated invocations of dvoImagesAtCoords each
of which needs to read the entire tessellation directory.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/getstar/src/dvoImagesAtCoords.c

    r33658 r36057  
    99static int makePoint(double ra, double dec, Point **pointsOut);
    1010static int ListImagesAtCoords (Image *dbImages, Point *points, int Npoints);
     11static int readPointFromFile(FILE *f, Point *pt);
    1112
    1213int main (int argc, char **argv) {
     
    2324 
    2425  Point *points;
     26  int readStdin = 0;
    2527  if (coordsFile) {
    26       Npoints = readPoints(coordsFile, &points);
     28      if (strcmp(coordsFile, "-") == 0) {
     29        readStdin = 1;
     30      } else {
     31        Npoints = readPoints(coordsFile, &points);
     32      }
    2733  } else {
    2834      Npoints = makePoint(cmd_line_ra, cmd_line_dec, &points);
    2935  }
    30   if (!Npoints) {
     36  if (!Npoints && !readStdin) {
    3137    exit(1);
    3238  }
     
    6167  }
    6268 
    63   if (MatchCoords (dbImages, NdbImages, points, Npoints)) {
    64     ListImagesAtCoords(dbImages, points, Npoints);
    65     exit(0);
     69  if (readStdin) {
     70    Point pt;
     71    memset(&pt, 0, sizeof(pt));
     72    pt.Nmatches = 0;
     73    ALLOCATE(pt.matches, Match, 20);
     74    pt.arrayLength = 20;
     75
     76    int status;
     77    while ((status = readPointFromFile(stdin, &pt))) {
     78      if (status < 0) {
     79        fprintf(stdout, "ERROR malformed input line\n");
     80        exit(1);
     81      }
     82      if (MatchCoords (dbImages, NdbImages, &pt, 1)) {
     83        ListImagesAtCoords(dbImages, &pt, 1);
     84      }
     85      fprintf(stdout, "DONE\n");
     86      fflush(stdout);
     87      pt.Nmatches = 0;
     88    }
    6689  } else {
    67     exit(PSTAMP_NO_OVERLAP);
    68   }
     90    if (MatchCoords (dbImages, NdbImages, points, Npoints)) {
     91      ListImagesAtCoords(dbImages, points, Npoints);
     92    } else {
     93      exit(PSTAMP_NO_OVERLAP);
     94    }
     95  }
     96  exit(0);
     97}
     98
     99static int readPointFromFile(FILE *f, Point *pt) {
     100    char buf[80];
     101    char *good = fgets(buf, 80, f);
     102    if (!good) {
     103        return 0;
     104    }
     105    // fprintf(stderr, "READ: %s\n", buf);
     106    int Nread = sscanf(buf, "%d %lf %lf\n", &pt->id, &pt->ra, &pt->dec);
     107    if (Nread == 3) {
     108        // got one
     109        return 1;
     110    } else if (Nread == -1) {
     111        // all done time to go
     112        return 0;
     113    } else {
     114        // fprintf(stderr, "malformed input line: %d\n", Nread);
     115        return -1;
     116    }
    69117}
    70118
  • trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm

    r36042 r36057  
    3434use POSIX;
    3535use Time::HiRes qw(gettimeofday);
     36use IO::Handle;
    3637
    3738my $dvo_verbose = 0;
     
    12881289
    12891290    my @lines;
    1290     {
     1291    my $looked_up_fast;
     1292    if ($requested_tess_id) {
     1293        # try using the last method first
     1294        $looked_up_fast = lookup_skycells_fast($ipprc, \@lines, $requested_tess_id, $ra, $dec, $verbose);
     1295    }
     1296   
     1297    if (!$looked_up_fast) {
    12911298        my $command = "$whichimage $ra $dec";
    12921299        $command .= " --tess_id $requested_tess_id" if $requested_tess_id;
     
    20282035    return $projection_cell;
    20292036}
     2037
     2038# lookup_skycells_fast()
     2039# Lookup using a "skycell server" process which is implemented by running dvoImagesAtCoords in "server" mode.
     2040# Repeated calls to this function will reuse an existing process. This avoids having to loading the
     2041# tessellation over and over again for each skycell lookup.
     2042# When this parse process exists, the pipes are closed which causes the skycell server to exit.
     2043
     2044my $scs_tess_id;    # tess_id for existing scs (skycell server) (if any)
     2045my $scs_out;        # reference filehandle for sending data to the scs
     2046my $scs_in;         # filehandle for response from scs
     2047
     2048sub lookup_skycells_fast {
     2049    my ($ipprc, $results, $tess_id, $ra, $dec, $verbose) = @_;
     2050
     2051    # see if we need to start the scs (skycell server)
     2052
     2053    if (!$scs_tess_id or ($scs_tess_id ne $tess_id)) {
     2054        if ($scs_tess_id) {
     2055            # tess_id has changed, close down existing scs
     2056            close $scs_out;
     2057            $scs_out = undef;
     2058            close $scs_in;
     2059            $scs_in = undef;
     2060            $scs_tess_id = undef;
     2061        }
     2062
     2063        # convert tess_id to a directory name
     2064        my $tess_dir = $ipprc->tessellation_catdir( $tess_id );
     2065        unless ($tess_dir) {
     2066            print STDERR "Unrecognized tess_id: $tess_id\n";
     2067            return 0;
     2068        }
     2069
     2070        # convert tess_dir to an absolute directory name
     2071        my $tess_dir_resolved = $ipprc->convert_filename_absolute( $tess_dir );
     2072        unless ($tess_dir_resolved and -d $tess_dir_resolved) {
     2073            print STDERR "failed to resolve tessellation directory for $tess_id\n";
     2074            return 0;
     2075        }
     2076
     2077        # start an scs
     2078
     2079        # create pipes for communicating with the server
     2080        pipe $scs_in, SERVER_WRITER;
     2081        pipe SERVER_READER, $scs_out;
     2082
     2083        # set our output to be unbuffered
     2084        $scs_out->autoflush(1);
     2085
     2086        # FORK
     2087        my $scs_pid = fork();
     2088
     2089        if (!$scs_pid) {
     2090            unless (defined $scs_pid) {
     2091                # This is still in parent process. No joy.
     2092                print STDERR "fork of skycell server failed: $!";
     2093                return 0;
     2094            }
     2095
     2096            # This code is running in the child process - the skycell server
     2097            # Close file handles for the parent's ends of the pipes
     2098            close $scs_in;
     2099            close $scs_out;
     2100            # close stdio filehandles. we are about to redirect them
     2101            # (STDERR stays open pointing to the parser log)
     2102            close STDIN;
     2103            close STDOUT;
     2104
     2105            # redirect our stdio file handles to the proper end of the pipes
     2106            open (STDIN,  "<&SERVER_READER")   or die "\nSCS: failed to redirect stdin";
     2107            open (STDOUT, ">>&SERVER_WRITER")  or die "\nSCS: failed to redirect stdout";
     2108
     2109            # All set. Now exec dvoImagesAtCoords
     2110
     2111            my $command = "$dvoImagesAtCoords -D CATDIR $tess_dir_resolved -coords -";
     2112
     2113            print STDERR "SCS: execing $command\n";
     2114
     2115            unless(exec $command) {
     2116                # note parent will notice that we are gone due to the pipes getting broken
     2117                # when this child dies.
     2118                die "SCS: failed to exec $command";
     2119            }
     2120        }
     2121
     2122        # This code is running in the parent process (the parser).
     2123        # We have succesfully launched the scs process.
     2124        # Close file handles for the scs' end of the pipes.
     2125        close SERVER_WRITER;
     2126        close SERVER_READER;
     2127
     2128        # remember the tess_id
     2129        $scs_tess_id = $tess_id;
     2130    }
     2131
     2132    # send coordinates to the skycell server and wait for the results
     2133    # (which come nearly instantly which is the point of doing this)
     2134
     2135    # If pipes to the skycell server die, don't abort.
     2136    # Our reads and writes detect errors and act sensibly.
     2137    local $SIG{PIPE} = 'IGNORE';
     2138
     2139    # write the coordinates to the pipe
     2140    my $write_ok = print $scs_out "1 $ra $dec\n";
     2141    if (!$write_ok) {
     2142        # server process probably didn't start properly or died. Fail.
     2143        # Caller will attempt to use the conventional method.
     2144        print STDERR "write to skycell server failed. Error is $!\n";
     2145        return 0;
     2146    }
     2147
     2148    my $very_verbose = 0;
     2149    print STDERR "  sent coordinates to skycell server\n" if $very_verbose;
     2150
     2151    my $received_done = 0;
     2152
     2153    # Wait for response. If pipe breaks the read returns with no data.
     2154    while (my $line = <$scs_in>) {
     2155        chomp $line;
     2156        if ($line eq 'DONE') {
     2157            # No more results for these coordinates.
     2158            $received_done = 1;
     2159            print STDERR "    received DONE\n" if $very_verbose;
     2160            last;
     2161        }
     2162        print STDERR "    received $line\n" if $very_verbose;
     2163
     2164        # The caller expects the output to be in the format used by whichimage
     2165        # which omits the point number and includes the tess_id.
     2166        my ($ptnum, $raout, $decout, $skycell_id) = split " ", $line;
     2167
     2168        my $out = "$raout $decout $tess_id $skycell_id";
     2169
     2170        # print the result to the log
     2171        print "$out\n" if $verbose;
     2172
     2173        push @$results, $out;
     2174    }
     2175
     2176    print STDERR "Out of wait for responses loop\n" if $very_verbose;
     2177
     2178    if (!$received_done) {
     2179        # something has gone wrong.
     2180        print STDERR "Read loop exited without receiving DONE.\n";
     2181
     2182        die "BAILING out" if $very_verbose;
     2183
     2184        # forget about the existing server instance
     2185        $scs_tess_id = undef;
     2186        close $scs_out;
     2187        close $scs_in;
     2188
     2189        # drop any results received so far
     2190        @$results = ();
     2191        return 0;
     2192    }
     2193
     2194    return 1;
     2195}
     2196
    20302197       
    20312198
Note: See TracChangeset for help on using the changeset viewer.