Example #1
0
DCBuffer * DCFilter::readany(std::string *exceptionPorts, int numExc, std::string * port) {
	char *inPortName;
	int i;

	// Reads the data using Anthill
	char **c_exceptionPorts = (char **) malloc(numExc);
	for(i = 0; i < numExc; i++) {
		c_exceptionPorts[i] = (char *) (exceptionPorts[i]).c_str();
	}
	int bufSz = dsReadBufferAnyPort(&inPortName, c_exceptionPorts, numExc, recvBuf->getBufPtr(), recvBuf->getSize());

	if(port != NULL)
		*port = inPortName;

	if (bufSz != EOW) {
		// Pack the data in a DCBuffer
		DCBuffer * dcBuf = new DCBuffer(bufSz);
		memcpy(dcBuf->getPtr(), recvBuf->getBufPtr(), bufSz);
		dcBuf->setUsedSize(bufSz);
		return dcBuf;
	}

	// Received an EOW. There isn't more data to be received.
	return NULL;
}
Example #2
0
// Returns NULL or non-NULL, depending on if anything is available or not
// when it is called.
DCBuffer * DCFilter::read_non_blocking(const std::string & portName) {
	// Gets Anthill input port handler
	InputPortHandler inPort = dsGetInputPortByName((char *) portName.c_str());

	if(inPort == -1) {
		string exitMsg = "read_non_blocking: There is no port "+portName+" in filter "+get_filter_name();
		dsExit((char *)exitMsg.c_str());
	}

	// Reads the data using Anthill
	int bufSz = dsReadNonBlockingBuffer(inPort, recvBuf->getBufPtr(), recvBuf->getSize());

	if (bufSz == 0) {
		// There is no data to be received now.
		return NULL;
	}

	if (bufSz != EOW) {
		// Pack the data in a DCBuffer
		DCBuffer * dcBuf = new DCBuffer(bufSz);
		memcpy(dcBuf->getPtr(), recvBuf->getBufPtr(), bufSz);
		dcBuf->setUsedSize(bufSz);
		return dcBuf;
	}

	// Received an EOW. There isn't more data to be received.
	return NULL;
}
Example #3
0
int filterA::process(){

	cout << "process " << get_filter_name() << " Id: " 
		<< get_global_filter_thread_id() << ". Rank: " << get_my_rank() << "\n";

	DCBuffer * outBuffer = new DCBuffer(100);
	outBuffer->pack("s", "Hellow!!!");

	write (outBuffer, "out");
	outBuffer->Delete();

	DCBuffer * outBuffer1 = new DCBuffer(100);
	outBuffer1->pack("s", "NonBlocking message.");

	write_nocopy(outBuffer1, "out");
	outBuffer1->Delete();	

	//filter_exit("Nao e' nada nao");

	return 0;
}
Example #4
0
 int process()
 {
     int i;
     int j;
     DCBuffer * work = get_init_filter_broadcast();
     std::string packet_size_and_num_packets;
     work->Extract(& packet_size_and_num_packets);
     std::vector<std::string> toks =
         dcmpi_string_tokenize(packet_size_and_num_packets);
     int num_packets = dcmpi_csnum(toks[1]);
     for (i = 0; i < num_packets; i++) {
         DCBuffer * b = readany();
         char * p = b->getPtr();
         for (j = 0; j < b->getUsedSize(); j++) {
             char expected = (char)(j%256);
             if (p[j] != expected) {
                 sleep(2);
                 std::cerr << "ERROR: expecting " << (int)expected
                           << ", got " << (int)p[j]
                           << ", j=" << j
                           << " at " << __FILE__ << ":" << __LINE__
                           << std::endl << std::flush;
                 std::cerr << *b;
                 // b->extended_print(cerr);
                 assert(0);
             }
         }
         b->consume();
         printf(".");
         fflush(stdout);
     }
     printf("\n");
     return 0;
 }
Example #5
0
 int process()
 {
     int i;
     int j;
     DCBuffer * work = get_init_filter_broadcast();
     std::string packet_size_and_num_packets;
     work->Extract(& packet_size_and_num_packets);
     std::vector<std::string> toks =
         dcmpi_string_tokenize(packet_size_and_num_packets);
     int packet_size = dcmpi_csnum(toks[0]);
     int num_packets = dcmpi_csnum(toks[1]);
     for (i = 0; i < num_packets; i++) {
         DCBuffer * b = new DCBuffer(packet_size);
         char * p = b->getPtr();
         for (j = 0; j < packet_size; j++) {
             p[j] = (char)(j%256);
         }
         b->setUsedSize(packet_size);
         writeany_nocopy(b);
     }
     return 0;
 }
void ocvm_tessellator::do_tess(DCBuffer * input)
{
    int4 sentinel;
    int8 width, height;
    int4 x, y, z;
    uint1 threshold;
    int4 color;
    input->unpack("iBlliiii", &sentinel, &threshold,
                  &width, &height, &x, &y, &z,
                  &color);
    const int hdrsz = 64;
    input->resetExtract(hdrsz);
    int8 cells_per_row = width / user_tessellation_x;
    if (width % user_tessellation_x) {
        cells_per_row++;
    }
    int8 total_cell_rows = height / user_tessellation_y;
    if (height % user_tessellation_y) {
        total_cell_rows++;
    }
    int new_sz = cells_per_row * total_cell_rows*sizeof(ocvm_sum_integer_type);
    DCBuffer * out = new DCBuffer(
        hdrsz + new_sz);
    memset(out->getPtr(), 0, hdrsz + new_sz);
    out->pack("iBlliiii", sentinel, threshold, cells_per_row, total_cell_rows,
              x, y, z, color);
//     std::cout << "tessellation: active on x,y,z of "
//               << x << "," << y << "," << z << ", color " << color
//               << " on host " << dcmpi_get_hostname(true) << endl;
    out->setUsedSize(hdrsz + new_sz);
    uint1 * src_base = (uint1*)input->getPtrExtract();
    uint1 * src = src_base;
    uint1 * src_end = src_base + width * height;
    ocvm_sum_integer_type * dest = (ocvm_sum_integer_type*)(out->getPtr()+hdrsz);
    ocvm_sum_integer_type * dest_end = (ocvm_sum_integer_type*)((char*)dest + new_sz);
    register ocvm_sum_integer_type count;
    int8 input_rows_covered = 0;
    int cell, cell_member;
    while (input_rows_covered < height) {
        assert (dest < dest_end);
        uint1 * row_end = src + width;
        for (cell = 0; cell < cells_per_row; cell++) {
            memcpy(&count, dest + cell, sizeof(ocvm_sum_integer_type));
            for (cell_member = 0;
                 cell_member < user_tessellation_x && src < row_end;
                 cell_member++) {
                count += *src;
                src++;
            }
            memcpy(dest + cell, &count, sizeof(ocvm_sum_integer_type));
        }
        input_rows_covered++;
        if (input_rows_covered % user_tessellation_y == 0) {
            dest += cells_per_row;
        }
    }
//     cout << "te2: " << *out << endl;
//     std::cout << "tess sending out ";
//     out->extended_print(cout);
    write_nocopy(out, "0");
    delete input;
}
int main(int argc, char *argv[])
{

    uint warp_filters_per_host = 1;
    std::string dest_host_scratch_filename;
    bool non_local_destination = 0;
    std::string client_hosts_filename;
    bool non_local_clients = 0;

    while (argc > 1) {
        if (!strcmp(argv[1], "-w")) {
                 warp_filters_per_host = atoi(argv[2]);
                 dcmpi_args_shift(argc, argv);
        }
        else if (!strcmp(argv[1], "-clients")) {
                 non_local_clients = 1;
                 client_hosts_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else if (!strcmp(argv[1], "-dest")) {
                 non_local_destination = 1;
                 dest_host_scratch_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }

    if ((argc-1) != 3) {
        appname = argv[0];
        usage();
    }

    HostScratch *dest_host_scratch = NULL;
    if (non_local_destination) {
        dest_host_scratch = new HostScratch(dest_host_scratch_filename);
    }
    if (non_local_destination && dest_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    HostScratch *client_host_scratch = NULL;
    if (non_local_clients) {
        client_host_scratch = new HostScratch(client_hosts_filename);
    }
    if (non_local_clients && client_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    if (!dcmpi_string_ends_with(tostr(argv[1]), ".xml")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[1])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    JibberXMLDescriptor *jxd = new JibberXMLDescriptor();
    jxd->init_from_file(argv[1]);
    cout << "delta = " << jxd->delta << endl;
    cout << "ncp = " << jxd->num_control_points << endl;

    if (!dcmpi_string_ends_with(tostr(argv[2]), ".dim")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[2])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    if (!dcmpi_string_ends_with(tostr(argv[3]), ".dim")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[3])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    std::string warp_filename = tostr(argv[3]);

    uint u, u2;
    int rc;
    ImageDescriptor descriptor;
    std::string image_descriptor_string = file_to_string(argv[2]);
    descriptor.init_from_string(image_descriptor_string);
    std::vector<std::string> input_hosts = descriptor.get_hosts();
    std::vector<std::string> hosts;

    std::map< std::string, std::string> src_to_dest_host, client_to_dest_host, client_to_src_host, src_to_client_host;
    if (non_local_destination && !non_local_clients) {
        assert(input_hosts.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
        for (u = 0; u < dest_host_scratch->components.size(); u++) {
            src_to_dest_host[input_hosts[u]] = (dest_host_scratch->components[u])[0];
        }
    }

    if (non_local_clients) {
        assert(input_hosts.size() == client_host_scratch->components.size());   // Assumption for now. Will change later
        for (u = 0; u < client_host_scratch->components.size(); u++) {
            client_to_src_host[(client_host_scratch->components[u])[0]] = input_hosts[u];
            src_to_client_host[input_hosts[u]] = (client_host_scratch->components[u])[0];
        }
        if (non_local_destination) {
            assert(client_host_scratch->components.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
            for (u = 0; u < client_host_scratch->components.size(); u++) {
                client_to_dest_host[(client_host_scratch->components[u])[0]] = (dest_host_scratch->components[u])[0];
            }
        }
        hosts = client_host_scratch->get_hosts();
    }
    else {
        hosts = input_hosts;
    }

    DCLayout layout;
    layout.use_filter_library("libocvmfilters.so");
    DCFilterInstance console ("<console>", "console");
    layout.add(console);

    std::vector< std::string> dest_hosts;
    std::vector< std::string> client_hosts;
    if (non_local_destination) {
        dest_hosts = dest_host_scratch->get_hosts();
    }
    if (non_local_clients) {
        client_hosts = client_host_scratch->get_hosts();
    }
    MediatorInfo info = mediator_setup(layout, 2, 1, input_hosts, client_hosts, dest_hosts);

    std::vector<DCFilterInstance*> computers;
    std::vector<DCFilterInstance*> mappers;
    std::vector<DCFilterInstance*> readalls;
    std::vector<DCFilterInstance*> mapper_readalls;
    std::vector<DCFilterInstance*> writers;
    for (u = 0; u < hosts.size(); u++) {
        for (u2 = 0; u2 < warp_filters_per_host; u2++) {
            std::string uniqueName = "W1_" + tostr(u) + "_" + tostr(u2);
            DCFilterInstance * warper =
                new DCFilterInstance("ocvm_warper", uniqueName);
            layout.add(warper);
            computers.push_back(warper);
            warper->bind_to_host(hosts[u]);
            warper->set_param("algo", "naive");
            warper->set_param("label", uniqueName);
            warper->set_param("myhostname", hosts[u]);
            warper->set_param("threadID", tostr(u2));
            warper->set_param("warp_filters_per_host", warp_filters_per_host);
            warper->set_param("delta", tostr(jxd->delta));
            warper->set_param("num_control_points", tostr(jxd->num_control_points));
//            warper->set_param("image_descriptor_string", tostr(descriptor));
            if (non_local_clients && non_local_destination) {
                warper->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
                warper->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
            }
            else if (non_local_destination && !non_local_clients) {
                warper->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
                warper->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
            }
            else {
                warper->set_param("dest_host_string", hosts[u]);
                if (!non_local_clients) {
                    warper->set_param("dest_scratchdir", "");
                }
                else {
                    warper->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
                }
            }

            layout.add_port(&console, "to_warper", warper, "from_console");

            if (u2 > 0) {
                continue;                               // only 1 warp mapper and writer per host
            }

            DCFilterInstance * mediator_readall =
                new DCFilterInstance("ocvm_mediator_readall_samehost",
                                     tostr("ra_") + tostr(hosts[u]));
            mediator_readall->bind_to_host(hosts[u]);
            readalls.push_back(mediator_readall);
//             mediator_readall->set_param("image_descriptor_string",
//                                         tostr(descriptor));
            layout.add(mediator_readall);

            DCFilterInstance * mapper_readall =
                new DCFilterInstance("ocvm_mediator_readall_samehost",
                                     tostr("mra_") + tostr(hosts[u]));
            mapper_readall->bind_to_host(hosts[u]);
            mapper_readalls.push_back(mapper_readall);
//             mapper_readall->set_param("image_descriptor_string",
//                                         tostr(descriptor));
            layout.add(mapper_readall);

            uniqueName = "WM_" + tostr(u);
            DCFilterInstance * warpmapper =
                new DCFilterInstance("ocvm_warp_mapper", uniqueName);
            layout.add(warpmapper);
            mappers.push_back(warpmapper);
            warpmapper->bind_to_host(hosts[u]);
            warpmapper->set_param("label", uniqueName);
            warpmapper->set_param("myhostname", hosts[u]);
            warpmapper->set_param("warp_filters_per_host", warp_filters_per_host);
//            warpmapper->set_param("image_descriptor_string", tostr(descriptor));

            layout.add_port(mapper_readall, "output",
                            warpmapper, "from_readall");
            layout.add_port(warpmapper, "ack",
                            mapper_readall, "ack");

            uniqueName = "WW_" + tostr(u);
            DCFilterInstance * warpwriter =
                new DCFilterInstance("ocvm_warp_writer", uniqueName);
            layout.add(warpwriter);
            writers.push_back(warpwriter);
            warpwriter->bind_to_host(hosts[u]);
            warpwriter->set_param("label", uniqueName);
            warpwriter->set_param("myhostname", hosts[u]);
            warpwriter->set_param("warp_filters_per_host", warp_filters_per_host);
//           warpwriter->set_param("image_descriptor_string", tostr(descriptor));
            if (non_local_clients && non_local_destination) {
                warpwriter->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
                warpwriter->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
            }
            else if (non_local_destination && !non_local_clients) {
                warpwriter->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
                warpwriter->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
            }
            else {
                warpwriter->set_param("dest_host_string", hosts[u]);
                if (!non_local_clients) {
                    warpwriter->set_param("dest_scratchdir", "");
                }
                else {
                    warpwriter->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
                }
            }

            if (non_local_clients) {
                warper->set_param("input_hostname", client_to_src_host[hosts[u]]);
                warpmapper->set_param("input_hostname", client_to_src_host[hosts[u]]);
                warpwriter->set_param("input_hostname", client_to_src_host[hosts[u]]);
            }
            else {
                warper->set_param("input_hostname", hosts[u]);
                warpmapper->set_param("input_hostname", hosts[u]);
                warpwriter->set_param("input_hostname", hosts[u]);
            }
    
            layout.add_port(warpwriter, "to_console", &console, "from_warpwriter");
        }
    }

    for (u = 0; u < readalls.size(); u++) {
        for (u2 = 0; u2 < warp_filters_per_host; u2++) {
            layout.add_port(readalls[u], "output",
                            computers[u*warp_filters_per_host + u2], "from_readall");
            layout.add_port(computers[u*warp_filters_per_host + u2], "ack",
                            readalls[u], "ack");
        }
    }

    for (u = 0; u < computers.size(); u++) {
        for (u2 = 0; u2 < mappers.size(); u2++) {                                                       // assumes 1 mapper and 1 writer per host
            if (computers[u]->get_param("myhostname") == writers[u2]->get_param("myhostname")) {
                layout.add_port(computers[u], "to_" + writers[u2]->get_param("myhostname"), writers[u2], "0");
            }
            layout.add_port(computers[u], "to_m_" + mappers[u2]->get_param("myhostname"), mappers[u2], "0");
        }
    }
    
    for (u = 0; u < mappers.size(); u++) {
        for (u2 = 0; u2 < writers.size(); u2++) {
            layout.add_port(mappers[u], "to_" + writers[u2]->get_param("myhostname"), writers[u2], "0");
        }
    }

    mediator_add_client(layout, info, readalls);
    mediator_add_client(layout, info, mapper_readalls);
    mediator_add_client(layout, info, writers);

    double before = dcmpi_doubletime();

    std::string dim_timestamp = get_dim_output_timestamp();
    layout.set_param_all("dim_timestamp", dim_timestamp);

    DCFilter * console_filter = layout.execute_start();
    DCBuffer * imgstr = new DCBuffer(image_descriptor_string.size()+1);
    imgstr->pack("s", image_descriptor_string.c_str());
    console_filter->write_broadcast(imgstr, "to_warper");
    delete imgstr;
    for (u = 0; u < jxd->num_control_points; u++) {
        DCBuffer * out = new DCBuffer(4 * sizeof(float) + sizeof(uint));
/*
        cout << jxd->correspondences[u]->origin_x <<  " " << 
                           jxd->correspondences[u]->origin_y <<  " " <<
                           jxd->correspondences[u]->endpoint_x <<  " " <<
                           jxd->correspondences[u]->endpoint_y <<  " " <<
                           jxd->correspondences[u]->weight << endl;
*/

        out->pack("ffffi", jxd->correspondences[u]->origin_x, 
                           jxd->correspondences[u]->origin_y, 
                           jxd->correspondences[u]->endpoint_x, 
                           jxd->correspondences[u]->endpoint_y, 
                           jxd->correspondences[u]->weight);
        console_filter->write_broadcast(out, "to_warper");
    }

    std::map<ImageCoordinate, std::pair<std::string, int8> > newfiles;
    for (u = 0; u < descriptor.parts.size(); u++) {
        DCBuffer * in = console_filter->read("from_warpwriter");
        ImageCoordinate ic;
        std::string output_filename;
        int8 output_offset;
        in->unpack("iiisl", &ic.x, &ic.y, &ic.z,
                   &output_filename, &output_offset);
        newfiles[ic] = make_pair(output_filename, output_offset);
        delete in;
    }

    rc = layout.execute_finish();
    if (rc) {
        std::cerr << "ERROR: layout.execute() returned " << rc
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    else {
        std::string message = "type BGRplanar\n";
        message += "pixels_x " + tostr(descriptor.pixels_x) + "\n";
        message += "pixels_y " + tostr(descriptor.pixels_y) + "\n";
        message += "pixels_z " + tostr(descriptor.pixels_z) + "\n";
        message += "chunks_x " + tostr(descriptor.chunks_x) + "\n";
        message += "chunks_y " + tostr(descriptor.chunks_y) + "\n";
        message += "chunks_z " + tostr(descriptor.chunks_z) + "\n";
        message += "chunk_dimensions_x";
        for (u = 0; u < descriptor.chunks_x; u++) {
            message += " ";
            message += tostr(descriptor.chunk_dimensions_x[u]);
        }
        message += "\n";
        message += "chunk_dimensions_y";
        for (u = 0; u < descriptor.chunks_y; u++) {
            message += " ";
            message += tostr(descriptor.chunk_dimensions_y[u]);
        }
        message += "\n";
        for (u = 0; u < descriptor.parts.size(); u++) {
            ImagePart & part = descriptor.parts[u];
            std::string & fn_old = part.filename;
            std::string fn_new = newfiles[part.coordinate].first;
            int8 offset_new = newfiles[part.coordinate].second;
            std::string output_hostname = part.hostname;
            if (non_local_destination && non_local_clients) {
                output_hostname = client_to_dest_host[src_to_client_host[part.hostname]];
            }
            if (non_local_destination && !non_local_clients) {
                output_hostname = src_to_dest_host[part.hostname];
            }
            if (!non_local_destination && non_local_clients) {
                output_hostname = src_to_client_host[part.hostname];
            }
            message += "part " + tostr(part.coordinate) + " " +
                output_hostname + " " + fn_new + " " + tostr(offset_new) + "\n";
        }
        message += "timestamp " + dcmpi_get_time() + "\n";

        FILE *f_wdim;
        if ((f_wdim = fopen(warp_filename.c_str(), "w")) == NULL) {
            std::cerr << "ERROR: opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fwrite(message.c_str(), message.size(), 1, f_wdim) < 1) {
            std::cerr << "ERROR: calling fwrite()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f_wdim) != 0) {
            std::cerr << "ERROR: calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
    }

    double after = dcmpi_doubletime();
    std::cout << "elapsed warp " << (after - before) << " seconds" << endl;

    return rc;
}
int answer_ps_queries(
    DCFilter * console_filter,
    const std::vector<std::string> & hosts,
    ImageDescriptor original_image_descriptor,
    ImageDescriptor prefix_sum_descriptor,
    std::vector<PixelReq> query_points,
    int zslice,
    std::vector<std::vector<int8> > & results)
{
    
    DCBuffer keep_going;
    keep_going.pack("i", 1);
    console_filter->write_broadcast(&keep_going, "0");

    DCBuffer out;
    out.pack("ssi",
             "psquery",
             tostr(prefix_sum_descriptor).c_str(),
             zslice);
    std::cout << "zslice is " << zslice << endl;
    console_filter->write_broadcast(&out, "0");

    // build cache
    int tess_x, tess_y;
    std::string extra = prefix_sum_descriptor.extra;
    std::vector<std::string> toks = dcmpi_string_tokenize(extra);
    tess_x = Atoi(toks[1]);
    tess_y = Atoi(toks[3]);
    std::string lpscache = toks[5];
    toks = dcmpi_string_tokenize(lpscache, ":");
    // fill lower-right-corner cache
    std::map<std::string, std::string> lower_right_corner_cache;
    for (uint u = 0; u < toks.size(); u++) {
        const std::string & s = toks[u];
        std::vector<std::string> toks2 = dcmpi_string_tokenize(s, "/");
        lower_right_corner_cache[toks2[0]] = toks2[1];
        std::cout << "cache: " << toks2[0] << "->" << toks2[1] << endl;
    }
    
    std::map<PixelReq, std::vector<int> > cached_contributions;
    std::map<PixelReq, std::vector<PixelReq> > querypoint_pspoints;

#define INSERT(x,y) request_set.insert(PixelReq(x,y)); querypoint_pspoints[*it].push_back(PixelReq(x,y)); std::cout << "inserted " << x << "," << y << "\n";

    SerialSet<PixelReq> request_set;
    std::vector<PixelReq>::iterator it;
    for (it = query_points.begin();
         it != query_points.end();
         it++) {
        int8 psx, psy;
        int8 new_x, new_y;

        psx = it->x / tess_x;
        psy = it->y / tess_y;

        int chunk_x, chunk_y;
        prefix_sum_descriptor.pixel_to_chunk(psx, psy, chunk_x, chunk_y);
        if (chunk_x != 0 && chunk_y != 0) {
            chunk_x--;
            chunk_y--;
            std::string key =
                tostr(chunk_x) + "," +
                tostr(chunk_y) + "," +
                tostr(zslice);
            std::string val = lower_right_corner_cache[key];
            std::vector<std::string> toks = dcmpi_string_tokenize(val, ",");
            std::vector<int> bgrvals;
            for (uint u = 0; u < toks.size(); u++) {
                bgrvals.push_back(Atoi(toks[u]));
            }
            cached_contributions[*it] = bgrvals;
            std::cout << "cached_contribution for query point "
                      << *it << " is "
                      << val << endl;
        }


        while (1) {
            if (prefix_sum_descriptor.bottommost_pixel_next_chunk_up(
                    psx, psy, new_x, new_y) == false) {
                break;
            }
            INSERT(new_x, new_y);
            psx = new_x;
            psy = new_y;
        }

        psx = it->x / tess_x;
        psy = it->y / tess_y;
        while (1) {
            if (prefix_sum_descriptor.rightmost_pixel_next_chunk_to_the_left(
                    psx, psy, new_x, new_y) == false) {
                break;
            }
            INSERT(new_x, new_y);
            psx = new_x;
            psy = new_y;
        }
        psx = it->x / tess_x;
        psy = it->y / tess_y;
        INSERT(psx, psy);
    }
    std::cout << "request_set: ";
    std::copy(request_set.begin(), request_set.end(), ostream_iterator<PixelReq>(cout, " ")); cout << endl;


    DCBuffer pointsbuf;
    request_set.serialize(&pointsbuf);
    console_filter->write_broadcast(&pointsbuf, "0");
    int4 scalar;
    int color = 0;
    int i;
    std::map<PixelReq, std::vector<int4> > fetch_results;
    for (i = 0; i < hosts.size(); i++) {
        DCBuffer * reply = console_filter->read("fromreader");
        std::string from_host;
        reply->unpack("s", &from_host);
        while (reply->getExtractAvailSize()) {
            PixelReq req;
            req.deSerialize(reply);
            std::vector<int4> tuple3;
            for (color = 0; color < 3; color++) {
                std::cout << "reply from host " << from_host
                          << ", pixelreq " << req
                          << ", color " << color << ": ";
                reply->unpack("i", &scalar);
                tuple3.push_back(scalar);
                std::cout << scalar << "\n";
            }
            fetch_results[req] = tuple3;
        }
        delete reply;
    }

    std::map<PixelReq, std::vector<int8> > sum_results;
    for (it = query_points.begin();
         it != query_points.end();
         it++) {
        const PixelReq & querypoint = *it;
        const std::vector<PixelReq> & needed_points =
            querypoint_pspoints[querypoint];
        std::vector<int8> single_sum_results;
        single_sum_results.push_back(0);
        single_sum_results.push_back(0);
        single_sum_results.push_back(0);
        uint np;
        for (np = 0; np < needed_points.size(); np++) {
            const PixelReq & needed_point = needed_points[np];
            std::vector<int> & lookup = fetch_results[needed_point];
            for (color = 0; color < 3; color++) {
                single_sum_results[color] += lookup[color];
            }
        }
        sum_results[querypoint] = single_sum_results;
        if (cached_contributions.count(querypoint) > 0) {
            std::cout << "cache hit for querypoint "
                      << querypoint << endl;
            std::vector<int> & cache = cached_contributions[querypoint];
            for (color = 0; color < 3; color++) {
                sum_results[querypoint][color] += cache[color];
                std::cout << "hit for color " << color
                          << ": " << cache[color] << endl;
            }
        }
        else {
            std::cout << "cache miss for querypoint "
                      << querypoint << endl;
        }
    }

    for (i = 0; i < query_points.size(); i++) {
        results.push_back(sum_results[query_points[i]]);
    }
    return 0;
}
int main(int argc, char * argv[])
{
    uint u;
    bool quiet = false;
    while (argc-1 >= 1) {
        if (!strcmp(argv[1], "-q")) {
            quiet = true;
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }
    
    if ((argc-1) < 1) {
        appname = argv[0];
        usage();
    }
    for (int i = 1; i < argc; i++) {
        std::string filename = argv[i];
        if (!dcmpi_file_exists(filename)) {
            std::cerr << "ERROR:  file " << filename
                      << " does not exist!"
                      << std::endl << std::flush;
            exit(1);
        }
        FILE * f;
        int len = ocvm_file_size(filename);
        char * contents = new char[len+1];
        contents[len] = 0;
        if ((f = fopen(filename.c_str(), "r")) == NULL) {
            std::cerr << "ERROR: opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fread(contents, len, 1, f) < 1) {
            std::cerr << "ERROR: calling fread()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f) != 0) {
            std::cerr << "ERROR: calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        ImageDescriptor original_image_descriptor;
        original_image_descriptor.init_from_string(contents);
        std::vector<std::string> hosts_vector =
            original_image_descriptor.get_hosts();

        DCLayout layout;
        DCFilterInstance console("<console>", "con");
        layout.add(console);
        layout.use_filter_library("libocvmfilters.so");
//         layout.add_propagated_environment_variable("DCMPI_FILTER_PATH", false);
        for (u = 0; u < hosts_vector.size(); u++) {
            DCFilterInstance * remover = new DCFilterInstance("ocvm_remover", tostr("remover") + tostr(u));
            layout.add(remover);
            if (quiet) {
                remover->set_param("quiet", "yes");
            }
            remover->set_param("my_hostname", hosts_vector[u]);
            remover->bind_to_host(hosts_vector[u]);
            layout.add_port(&console, "out", remover, "in");
        }
        DCFilter * console_filter = layout.execute_start();
        DCBuffer * imgstr = new DCBuffer();
        imgstr->pack("s", contents);
        delete[] contents;
        console_filter->write_broadcast(imgstr, "out");  
        delete imgstr;
        int rc = layout.execute_finish();
        if (rc == 0) {
            rc = remove(filename.c_str());
        }
        if (rc) {
            std::cerr << "ERROR: " << rc << " in execute() or remove()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (!quiet) {
            std::cout << "removed " << filename << endl;
        }
    }
    return 0;
}
int main(int argc, char *argv[])
{
    std::string dest_host_scratch_filename;
    bool non_local_destination = 0;
    std::string client_hosts_filename;
    bool non_local_clients = 0;

    while (argc > 1) {
        if (!strcmp(argv[1], "-dest")) {
                 non_local_destination = 1;
                 dest_host_scratch_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else if (!strcmp(argv[1], "-clients")) {
                 non_local_clients = 1;
                 client_hosts_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }

    appname = argv[0];
    if ((argc-1) != 5) {
        usage();
    }

    HostScratch *dest_host_scratch = NULL;
    if (non_local_destination) {
        dest_host_scratch = new HostScratch(dest_host_scratch_filename);
    }
    if (non_local_destination && dest_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    HostScratch *client_host_scratch = NULL;
    if (non_local_clients) {
        client_host_scratch = new HostScratch(client_hosts_filename);
    }
    if (non_local_clients && client_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    if (!dcmpi_string_ends_with(tostr(argv[1]), ".dim")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[1])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    if (!dcmpi_string_ends_with(tostr(argv[2]), ".dim")) {
        std::cerr << "ERROR: invalid filename " << tostr(argv[2])
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    int xs = atoi(argv[3]);
    int ys = atoi(argv[4]);
    int zs = atoi(argv[5]);
    if (xs < 1 || ys < 1 || zs < 1) {
        usage();
    }
    uint u, u2;
    int rc;
    DCLayout layout;
    layout.use_filter_library("libocvmfilters.so");
    DCFilterInstance console ("<console>", "console");
    layout.add(console);

    ImageDescriptor original_image_descriptor;
    original_image_descriptor.init_from_file(argv[1]);
    std::vector<std::string> input_hosts = original_image_descriptor.get_hosts();
    std::vector<std::string> hosts;

    std::map< std::string, std::string> src_to_dest_host, client_to_dest_host, client_to_src_host, src_to_client_host;
    if (non_local_destination && !non_local_clients) {
        assert(input_hosts.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
        for (u = 0; u < dest_host_scratch->components.size(); u++) {
            src_to_dest_host[input_hosts[u]] = (dest_host_scratch->components[u])[0];
        }
    }

    if (non_local_clients) {
        assert(input_hosts.size() == client_host_scratch->components.size());   // Assumption for now. Will change later
        for (u = 0; u < client_host_scratch->components.size(); u++) {
            client_to_src_host[(client_host_scratch->components[u])[0]] = input_hosts[u];
            src_to_client_host[input_hosts[u]] = (client_host_scratch->components[u])[0];
        }
        if (non_local_destination) {
            assert(client_host_scratch->components.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
            for (u = 0; u < client_host_scratch->components.size(); u++) {
                client_to_dest_host[(client_host_scratch->components[u])[0]] = (dest_host_scratch->components[u])[0];
            }
        }
        hosts = client_host_scratch->get_hosts();
    }
    else {
        hosts = input_hosts;
    }
    
    std::vector<DCFilterInstance*> rangefetchers;
    std::vector<DCFilterInstance*> scalers;
    for (u = 0; u < hosts.size(); u++) {
        std::string uniqueName = "SC" + tostr(u);

        DCFilterInstance * rangefetcher =
            new DCFilterInstance("ocvm_mediator_rangefetcher",
                                 uniqueName + "_f");
        layout.add(rangefetcher);
        rangefetchers.push_back(rangefetcher);
        rangefetcher->bind_to_host(hosts[u]);

        DCFilterInstance * scaler =
            new DCFilterInstance("ocvm_scaler", uniqueName + "_cxx");
        layout.add(scaler);
        scalers.push_back(scaler);
        scaler->bind_to_host(hosts[u]);
        scaler->set_param("desc", tostr(original_image_descriptor));
        if (non_local_clients && non_local_destination) {
            scaler->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
            scaler->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
        }
        else if (non_local_destination && !non_local_clients) {
            scaler->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
            scaler->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
        }
        else {
            scaler->set_param("dest_host_string", hosts[u]);
            if (!non_local_clients) {
                scaler->set_param("dest_scratchdir", "");
            }
            else {
                scaler->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
            }
        }

        if (non_local_clients) {
            scaler->set_param("input_hostname", client_to_src_host[hosts[u]]);
        }
        else {
            scaler->set_param("input_hostname", hosts[u]);
        }

        layout.add_port(rangefetcher, "0",
                        scaler, "from_rangefetcher");
        layout.add_port(scaler, "to_rangefetcher",
                        rangefetcher, "0");
        layout.add_port(scaler, "to_console", &console, "from_scaler");
    }
    std::vector< std::string> dest_hosts;
    std::vector< std::string> client_hosts;
    if (non_local_destination) {
        dest_hosts = dest_host_scratch->get_hosts();
    }
    if (non_local_clients) {
        client_hosts = client_host_scratch->get_hosts();
    }
    MediatorInfo info = mediator_setup(layout, 1, 1, input_hosts, client_hosts, dest_hosts);
    mediator_add_client(layout, info, scalers);
    mediator_add_client(layout, info, rangefetchers);
    double before = dcmpi_doubletime();
    std::string dim_timestamp = get_dim_output_timestamp();
    layout.set_param_all("dim_timestamp", dim_timestamp);
    layout.set_param_all("xs", tostr(xs));
    layout.set_param_all("ys", tostr(ys));
    layout.set_param_all("zs", tostr(zs));

    DCFilter * console_filter = layout.execute_start();
    std::map<ImageCoordinate, std::vector<std::string> > newfiles;
    int newparts = xs*ys*zs*
        original_image_descriptor.chunks_x*
        original_image_descriptor.chunks_y*
        original_image_descriptor.chunks_z;
    std::cout << "newparts " << newparts << endl;
    for (u = 0; u < newparts; u++) {
        DCBuffer * in = console_filter->read("from_scaler");
        ImageCoordinate ic;
        std::string hn;
        std::string output_filename;
        int8 output_offset;
        in->unpack("siiisl", &hn, &ic.x, &ic.y, &ic.z,
                   &output_filename, &output_offset);
        std::vector<std::string> v;
        v.push_back(hn);
        v.push_back(output_filename);
        v.push_back(tostr(output_offset));
        newfiles[ic] = v;
        delete in;
    }

    rc = layout.execute_finish();
    if (rc) {
        std::cerr << "ERROR: layout.execute() returned " << rc
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    else {
        int i;
        std::string message = "type BGRplanar\n";
        message += "pixels_x " + tostr(original_image_descriptor.pixels_x*xs)+"\n";
        message += "pixels_y " + tostr(original_image_descriptor.pixels_y*ys)+"\n";
        message += "pixels_z " + tostr(original_image_descriptor.pixels_z*zs)+"\n";
        message += "chunks_x " + tostr(original_image_descriptor.chunks_x*xs)+"\n";
        message += "chunks_y " + tostr(original_image_descriptor.chunks_y*ys)+"\n";
        message += "chunks_z " + tostr(original_image_descriptor.chunks_z*zs)+"\n";

        message += "chunk_dimensions_x";
        for (i = 0; i < xs; i++) {
            for (u = 0; u < original_image_descriptor.chunks_x; u++) {
                message += " ";
                message += tostr(original_image_descriptor.chunk_dimensions_x[u]);
            }
        }
        message += "\n";
        message += "chunk_dimensions_y";
        for (i = 0; i < ys; i++) {
            for (u = 0; u < original_image_descriptor.chunks_y; u++) {
                message += " ";
                message += tostr(original_image_descriptor.chunk_dimensions_y[u]);
            }
        }
        message += "\n";
        std::map<ImageCoordinate, std::vector<std::string> >::iterator it;
        for (it = newfiles.begin();
             it != newfiles.end();
             it++) {
            std::string hn_new = it->second[0];
            std::string fn_new = it->second[1];
            std::string offset_new = it->second[2];
            message += "part " + tostr(it->first) + " " +
                hn_new + " " + fn_new + " " + offset_new + "\n";
        }
        message += "timestamp " + dcmpi_get_time() + "\n";

        FILE *fout;
        if ((fout = fopen(argv[2], "w")) == NULL) {
            std::cerr << "ERROR: opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fwrite(message.c_str(), message.size(), 1, fout) < 1) {
            std::cerr << "ERROR: calling fwrite()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(fout) != 0) {
            std::cerr << "ERROR: calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
    }

    double after = dcmpi_doubletime();
    std::cout << "elapsed scaler " << (after - before) << " seconds" << endl;

    return rc;
}
int main(int argc, char * argv[])
{
    appname = strdup(dcmpi_file_basename(argv[0]).c_str());
    int i;
    int i2;
    uint u;
    int align_z_slice = 0;
    std::string channelOfInterest;
    std::string input_filename;
    std::string output_filename;
    double time_start = dcmpi_doubletime();    
    int rc;
    std::string execution_line;
    std::string client_hosts_filename;
    bool non_local_clients = 0;
    bool non_local_destination = 0;
    
    // printout arguments
    cout << "executing: ";
    for (i = 0; i < argc; i++) {
        if (i) {
            execution_line += " ";
        }
        execution_line += argv[i];
    }
    cout << execution_line << endl;

    while (argc > 1) {
        if (!strcmp(argv[1], "-clients")) {
                 non_local_clients = 1;
                 client_hosts_filename = argv[2];
                 dcmpi_args_shift(argc, argv);
        }
        else {
            break;
        }
        dcmpi_args_shift(argc, argv);
    }

    if ((argc-1) != 6) {
        usage();
    }
    input_filename = argv [1];
    channelOfInterest = argv [2];
    output_filename = argv [3];
    if (channelOfInterest != "R" &&
        channelOfInterest != "G" &&
        channelOfInterest != "B") {
        usage();
    }
    int subimage_width = dcmpi_csnum(argv[4]);
    int subimage_height = dcmpi_csnum(argv[5]);
    double subimage_overlap = atof(argv[6]);

    HostScratch *dest_host_scratch = NULL;
    HostScratch *client_host_scratch = NULL;
    if (non_local_clients) {
        client_host_scratch = new HostScratch(client_hosts_filename);
    }
    if (non_local_clients && client_host_scratch->components.empty()) {
        std::cerr << "ERROR:  destination host file is empty, aborting"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    if ((dcmpi_string_ends_with(input_filename, ".dim") ||
         dcmpi_string_ends_with(input_filename, ".DIM")) &&
        dcmpi_file_exists(input_filename)) {
        ;
    }
    else {
        std::cerr << "ERROR: invalid input filename " << input_filename
                  << std::endl << std::flush;
        exit(1);
    }
    
    DCLayout layout;
    layout.use_filter_library("libocvmfilters.so");
    layout.use_filter_library("/home/vijayskumar/projects/ocvm/src/ocvmjavafilters.jar");
    layout.add_propagated_environment_variable("OCVMSTITCHMON");
    layout.add_propagated_environment_variable("DCMPI_JAVA_CLASSPATH");

    ImageDescriptor original_image_descriptor;
    original_image_descriptor.init_from_file(input_filename);
    std::vector<std::string> input_hosts = original_image_descriptor.get_hosts();
    std::vector<std::string> hosts;

    std::map< std::string, std::string> src_to_dest_host, client_to_dest_host, client_to_src_host, src_to_client_host;
    if (non_local_destination && !non_local_clients) {
        assert(input_hosts.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
        for (u = 0; u < dest_host_scratch->components.size(); u++) {
            src_to_dest_host[input_hosts[u]] = (dest_host_scratch->components[u])[0];
        }
    }

    if (non_local_clients) {
        assert(input_hosts.size() == client_host_scratch->components.size());   // Assumption for now. Will change later
        for (u = 0; u < client_host_scratch->components.size(); u++) {
            client_to_src_host[(client_host_scratch->components[u])[0]] = input_hosts[u];
            src_to_client_host[input_hosts[u]] = (client_host_scratch->components[u])[0];
        }
        if (non_local_destination) {
            assert(client_host_scratch->components.size() == dest_host_scratch->components.size());     // Assumption for now. Will change later
            for (u = 0; u < client_host_scratch->components.size(); u++) {
                client_to_dest_host[(client_host_scratch->components[u])[0]] = (dest_host_scratch->components[u])[0];
            }
        }
        hosts = client_host_scratch->get_hosts();
    }
    else {
        hosts = input_hosts;
    }

    DCFilterInstance console ("<console>", "console");
    layout.add(console);
    
    DCFilterInstance maximum_spanning_tree("ocvm_maximum_spanning_tree2","MST");
    layout.add(maximum_spanning_tree);
    maximum_spanning_tree.bind_to_host(hosts[0]); // run it somewhere
    layout.add_port(maximum_spanning_tree, "to_console", console, "from_mst");
    
    std::vector<DCFilterInstance*> java_aligners;
    std::vector<DCFilterInstance*> cxx_aligners;
    std::vector<DCFilterInstance*> cxx_buddies;
    for (u = 0; u < hosts.size(); u++) {
        std::string hostname = (hosts[u]);
        std::string uniqueName = tostr("A_") + hostname;

        DCFilterInstance * java_aligner =
            new DCFilterInstance("ocvm_java_aligner", uniqueName + "_java");
        layout.add(java_aligner);
        java_aligners.push_back(java_aligner);
        java_aligner->bind_to_host(hostname);
        
        DCFilterInstance * cxx_aligner =
            new DCFilterInstance("ocvm_cxx_aligner", uniqueName + "_cxx");
        layout.add(cxx_aligner);
        cxx_aligners.push_back(cxx_aligner);
        cxx_aligner->bind_to_host(hostname);
//        cxx_aligner->set_param(
//            "image_descriptor_string", tostr(original_image_descriptor));

        DCFilterInstance * cxx_buddy =
            new DCFilterInstance("ocvm_cxx_aligner_buddy",
                                 uniqueName + "_bud");
        layout.add(cxx_buddy);
        cxx_buddies.push_back(cxx_aligner);
        cxx_buddy->bind_to_host(hostname);

        if (non_local_clients && non_local_destination) {
            cxx_aligner->set_param("dest_host_string", client_to_dest_host[hosts[u]]);
            cxx_aligner->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(client_to_dest_host[hosts[u]]));
        }
        else if (non_local_destination && !non_local_clients) {
            cxx_aligner->set_param("dest_host_string", src_to_dest_host[hosts[u]]);
            cxx_aligner->set_param("dest_scratchdir", dest_host_scratch->get_scratch_for_host(src_to_dest_host[hosts[u]]));
        }
        else {
            cxx_aligner->set_param("dest_host_string", hosts[u]);
            if (!non_local_clients) {
                cxx_aligner->set_param("dest_scratchdir", "");
            }
            else {
                cxx_aligner->set_param("dest_scratchdir", client_host_scratch->get_scratch_for_host(hosts[u]));
            }
        }
        if (non_local_clients) {
            cxx_aligner->set_param("input_hostname", client_to_src_host[hosts[u]]);
        }
        else {
            cxx_aligner->set_param("input_hostname", hosts[u]);
        }

        layout.add_port(&console, "to_cxx_aligner", cxx_aligner, "from_console");

        layout.add_port(cxx_aligner, "to_buddy", cxx_buddy, "0");
        layout.add_port(cxx_buddy, "to_j", java_aligner, "0");
        layout.add_port(java_aligner, "0", cxx_buddy, "from_j");

        layout.add_port(cxx_buddy, "to_mst",
                        &maximum_spanning_tree, "from_aligners");
    }

    std::vector< std::string> dest_hosts;
    std::vector< std::string> client_hosts;
    if (non_local_destination) {
        dest_hosts = dest_host_scratch->get_hosts();
    }
    if (non_local_clients) {
        client_hosts = client_host_scratch->get_hosts();
    }
    MediatorInfo info = mediator_setup(layout, 2, 1, input_hosts, client_hosts, dest_hosts);
    mediator_add_client(layout, info, cxx_aligners);

    layout.set_param_all("channelOfInterest", channelOfInterest);
    layout.set_param_all("numAligners", tostr(cxx_aligners.size()));
    layout.set_param_all(
        "nXChunks", tostr(original_image_descriptor.chunks_x));
    layout.set_param_all(
        "nYChunks", tostr(original_image_descriptor.chunks_y));
    layout.set_param_all("subimage_width", tostr(subimage_width));
    layout.set_param_all("subimage_height", tostr(subimage_height));
    layout.set_param_all("subimage_overlap", tostr(subimage_overlap));
    
    if (getenv("OCVMSTITCHMON")) {
        std::vector<std::string> tokens = dcmpi_string_tokenize(
            getenv("OCVMSTITCHMON"), ":");
        int s = ocvmOpenClientSocket(tokens[0].c_str(), Atoi(tokens[1]));
        std::string message = "setup " +
            tostr(original_image_descriptor.chunks_x) + " " +
            tostr(original_image_descriptor.chunks_y) + "\n";
        rc = ocvm_write_message(s, message);
        if (rc) {
            std::cerr << "ERROR: " << rc << " writing to socket "
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
        }
        close(s);
    }
    
    DCFilter * console_filter = layout.execute_start();
    std::string image_descriptor_string = tostr(original_image_descriptor);
    DCBuffer * imgstr = new DCBuffer(image_descriptor_string.size()+1);
    imgstr->pack("s", image_descriptor_string.c_str());
    console_filter->write_broadcast(imgstr, "to_cxx_aligner");
    delete imgstr;

    DCBuffer * finalized_offsets = console_filter->read("from_mst");
    
    int reps = 0;
    int8 maxX, maxY;
    int8 offset_x, offset_y;
    finalized_offsets->unpack("ll", &maxX, &maxY);
    std::string finalized_offsets_str = "finalized_offsets " +
        tostr(maxX) + "/" + tostr(maxY);
    while (finalized_offsets->getExtractAvailSize() > 0) {
        finalized_offsets_str += ",";
        finalized_offsets->unpack("ll", &offset_x, &offset_y);
        finalized_offsets_str += tostr(offset_x) + ":" + tostr(offset_y);
        reps++;
    }
    rc = layout.execute_finish();
    if (rc) {
        std::cerr << "ERROR: layout.execute() returned " << rc
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    else {
        FILE * f;
        if ((f = fopen(output_filename.c_str(), "w")) == NULL) {
            std::cerr << "ERROR: errno=" << errno << " opening file"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fwrite(finalized_offsets_str.c_str(),
                   finalized_offsets_str.size(), 1, f) < 1) {
            std::cerr << "ERROR: errno=" << errno << " calling fwrite()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f) != 0) {
            std::cerr << "ERROR: errno=" << errno << " calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
    }

    std::cout << "elapsed autoalign "
              << dcmpi_doubletime() - time_start << " seconds\n";
    return rc;
}
int ocvm_dim_writer2::process()
{
    std::cout << "dim writer on "
              << get_bind_host()
              << " launching\n" << flush;

    DCBuffer * in;
    std::string output_filename;
    std::string mode;
    std::vector<std::string> dirs_made;
    std::vector<std::string> dirs_rename_to;
    while (1) {
        in = read_until_upstream_exit("0");
        if (!in) {
           break;
        }
        in->unpack("ss", &output_filename, &mode);
        std::string d = dcmpi_file_dirname(output_filename);
        std::string tstamp = dcmpi_file_basename(d);
        std::string scratch_dir = dcmpi_file_dirname(d);
        std::string temporary_dir = scratch_dir + "/.tmp." + tstamp;
        std::string new_filename =
            temporary_dir + "/" + dcmpi_file_basename(output_filename);
        
        if (!dcmpi_file_exists(temporary_dir)) {
            if (dcmpi_mkdir_recursive(temporary_dir)) {
                if (errno != EEXIST) {
                    std::cerr << "ERROR: making directory " << temporary_dir
                              << " on " << dcmpi_get_hostname()
                              << " at " << __FILE__ << ":" << __LINE__
                              << std::endl << std::flush;
                    exit(1);
                }
            }
            else {                
                dirs_made.push_back(temporary_dir);
                dirs_rename_to.push_back(scratch_dir + "/" + tstamp);
            }
        }
        assert(dcmpi_file_exists(temporary_dir));

        FILE * f;
        if ((f = fopen(new_filename.c_str(), mode.c_str())) == NULL) {
            std::cerr << "ERROR: opening " << new_filename
                      << " for mode " << mode
                      << " on host " << dcmpi_get_hostname()
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fwrite(in->getPtrExtract(), in->getExtractAvailSize(), 1, f) < 1) {
            std::cerr << "ERROR: calling fwrite()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        if (fclose(f) != 0) {
            std::cerr << "ERROR: calling fclose()"
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }
        delete in;
    }

    // barrier
    int nwriters = get_param_as_int("nwriters");
    if (nwriters > 1) {
        DCBuffer broadcast;
        write_broadcast(&broadcast, "barrier");
        for (int i = 0; i < nwriters-1; i++) {
            DCBuffer* input = read ("barrier");
            delete input;
        }
    }
    
    for (unsigned int u = 0; u < dirs_made.size(); u++) {
        rename(dirs_made[u].c_str(), dirs_rename_to[u].c_str());
    }

    std::cout << "dim writer on "
              << get_bind_host()
              << " exiting\n";
    
    return 0;
}
int ocvm_dim_writer::process()
{
    std::string output_directory;
    int x1, y1, x2, y2, z;
    int i, j;
    DCBuffer * in;
    const char * mode;
    //while (1) {
        //in = read_until_upstream_exit("0");
        in = read("0");
        //if (!in) {
        //    break;
        //}
        in->unpack("siiiii", 
                   &output_directory, &x1, &y1,
                   &x2, &y2, &z);
        in->consume();
//         cout << "dimwriter on " << dcmpi_get_hostname()
//              << ": writing to "
//              << output_filename << endl;
        std::string containing_dir = dcmpi_file_dirname(output_directory);
        if (!dcmpi_file_exists(containing_dir)) {
            if (dcmpi_mkdir_recursive(containing_dir)) {
                std::cerr << "ERROR: making directories on "
                          << dcmpi_get_hostname()
                          << " at " << __FILE__ << ":" << __LINE__
                          << std::endl << std::flush;
            }
        }
        assert(dcmpi_file_exists(containing_dir));

        FILE * f;
        mode = "w";

        for (i = y1; i <= y2; i++) {
            for (j = x1; j <= x2; j++) {
                std::string output_filename = output_directory + tostr(j) + "_" + tostr(i) + "_0";
                if ((f = fopen(output_filename.c_str(), mode)) == NULL) {
                    std::cerr << "ERROR: opening " << output_filename
                              << " for mode " << mode
                              << " on host " << dcmpi_get_hostname()
                              << " at " << __FILE__ << ":" << __LINE__
                              << std::endl << std::flush;
                    exit(1);
                }

                in = read("0");
                if (compress) {
                    in->decompress();
                }

                if (fwrite(in->getPtr(), in->getUsedSize(), 1, f) < 1) {
                    std::cerr << "ERROR: calling fwrite()"
                              << " at " << __FILE__ << ":" << __LINE__
                              << std::endl << std::flush;
                    exit(1);
                }

                in->consume();
                
                if (fclose(f) != 0) {
                    std::cerr << "ERROR: calling fclose()"
                              << " at " << __FILE__ << ":" << __LINE__
                              << std::endl << std::flush;
                    exit(1);
                }
            }
        }

    //}
    return 0;
}
int ocvmqueryhandler::process()
{
    std::string my_workload = get_param("my_workload");
    off_t color_offset = (off_t)Atoi(get_param("color_offset"));
    off_t row_size = (off_t)Atoi(get_param("num_columns"));
    int partial = Atoi(get_param("partial"));
#ifdef DEBUG
//    cout << dcmpi_get_hostname() << " " <<  get_distinguished_name() << " workload: \n" << my_workload << endl;
#endif
    DCBuffer *outb;

    std::vector< std::string> lines = str_tokenize(my_workload, "\n");
    outb = new DCBuffer(lines.size()*sizeof(ocvm_sum_integer_type)*250);
    outb->Append(get_param("myhostname"));

    typedef std::map< std::string, std::map< off_t, std::string > >
        offset_value_mapping ;
    offset_value_mapping offset_value;

    for (int i = 0; i < (int)lines.size(); i++) {
        std::vector< std::string> tokens = str_tokenize(lines[i]);
        std::string filename = tokens[0];

        if (offset_value.count(filename) == 0) {
            offset_value[filename] =
                std::map< off_t, std::string >();
        }

        FILE * psumf = fopen(filename.c_str(),"r");
        if (!psumf) {
            std::cerr << "ERROR: opening file " << filename
                      << " on host " << dcmpi_get_hostname(true)
                      << " at " << __FILE__ << ":" << __LINE__
                      << std::endl << std::flush;
            exit(1);
        }

        for (int j = 1; j < (int)tokens.size(); j++) {
            off_t chunk_offset = (off_t)Atoi(tokens[j]);
            ocvm_sum_integer_type value_at_point;
            std::string values_at_point_string = "";

            std::map< off_t, std::string > &
                value_mapping = offset_value[filename];

            if (value_mapping.count(chunk_offset) == 0) {
                for (int k = 0; k < 3; k++) {
                    off_t seek_offset;
                    seek_offset = (chunk_offset + k * color_offset);
                    if (fseeko(psumf, seek_offset, SEEK_SET) != 0) {
                        std::cerr << "ERROR: seeking to position "
                                  << chunk_offset << " in file "
                                  << filename
                                  << " at " << __FILE__ << ":" << __LINE__
                                  << std::endl << std::flush;
                        exit(1);
                    }
                    if (fread(&value_at_point, sizeof(ocvm_sum_integer_type),
                              1, psumf) < 1) {
                        std::cerr << "ERROR: calling fread"
                                  << " at " << __FILE__
                                  << ":" << __LINE__
                                  << std::endl << std::flush;
                        exit(1);
                    }
                    outb->Append(value_at_point);		
                    values_at_point_string += tostr(value_at_point) + " ";
                }
                value_mapping[chunk_offset] = values_at_point_string;
            }
            else {
                std::vector< std::string > values_at_point = str_tokenize(value_mapping[chunk_offset]);
                for (int k = 0; k < 3; k++) {
                    value_at_point = (ocvm_sum_integer_type)Atoi(values_at_point[k]);
                    outb->Append(value_at_point);
                }
            }
        }
	
        fclose(psumf);
    }

    write(outb, "0");
    return 0;
}
int ocvm_ppm_partitioner::process()
{
    cout << "ocvm_ppm_partitioner: invoked \n";
    double before = dcmpi_doubletime();
    uint u;
    FILE * f_dim;
    int i, j;
    std::string input_filename = get_param("input_filename");
    std::string host_scratch_filename = get_param("host_scratch_filename");
    HostScratch host_scratch(host_scratch_filename);
    std::string output_filename = get_param("output_filename");
    std::string dim_timestamp = get_param("dim_timestamp");
    int nnodes = host_scratch.components.size();

    PPMDescriptor ppm(input_filename);
    if (ppm.pixels_y < nnodes) {
        std::cerr << "ERROR: can't handle such a tiny image"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }

    std::cout << "ppm.pixels_x: " << ppm.pixels_x << endl;
    std::cout << "ppm.pixels_y: " << ppm.pixels_y << endl;
    int output_tiles = nnodes;
    int8 sz = 3 * ppm.pixels_x * ppm.pixels_y;
    while ((sz / output_tiles) > MB_2) {
        output_tiles *= 2;
    }
    std::cout << "output_tiles " << output_tiles << endl;
    int chunks_per_host = output_tiles / nnodes;
    int pixel_rows_per_tile_norm = ppm.pixels_y / output_tiles;
    int pixel_rows_per_tile_rem = ppm.pixels_y % output_tiles;
    int pixel_rows_per_tile_last = ppm.pixels_y - (pixel_rows_per_tile_norm *
                                                   (output_tiles-1));
    std::cout << pixel_rows_per_tile_norm << endl;
    std::cout << pixel_rows_per_tile_rem << endl;
    std::cout << pixel_rows_per_tile_last << endl;

    int hostid = 0;
    int ckthisid = 0;
    std::vector<int> dims_y;
    std::vector<std::string> parts;
    for (int t = 0; t < output_tiles; t++) {
        int pixel_rows = pixel_rows_per_tile_norm;
        if (t == output_tiles-1) {
            pixel_rows = pixel_rows_per_tile_last;
        }
        else if (pixel_rows_per_tile_rem) {
            pixel_rows_per_tile_rem--;
            pixel_rows += 1;
            pixel_rows_per_tile_last--;
        }
        dims_y.push_back(pixel_rows);
        //std::cout << "malloc of "<<(pixel_rows * ppm.pixels_x * 3)<<endl;
        unsigned char * data_rgb =
            new unsigned char[pixel_rows * ppm.pixels_x * 3];
        std::string output_prefix =
            (host_scratch.components[hostid])[1] + "/" +
            dim_timestamp + "/" + dcmpi_file_basename(input_filename)
            + ".";
        int x = 0;
        int y = t;
        std::string output_fn =
            output_prefix + "x" +
            tostr(x) + "_y" +
            tostr(y);
        char partline[4096];
        snprintf(partline, sizeof(partline),
                "part %d %d %d %s %s",
                x,
                y, 
                0,
                ((host_scratch.components[hostid])[0]).c_str(),
                 output_fn.c_str());
        parts.push_back(partline);
        int npixels = pixel_rows * ppm.pixels_x;
        int chunk_sz = npixels*3;
        DCBuffer * outb = new DCBuffer(8+output_fn.size() + 8+1+
                                       chunk_sz);
        outb->pack("ss", output_fn.c_str(), "w");
        ppm.getrows(pixel_rows, data_rgb);

        // convert from RGB to planar BGR
        unsigned char * dest = (unsigned char*)outb->getPtrFree();
        for (int chan = 0; chan < 3; chan++) {
            unsigned char * src = data_rgb + (2 - chan);
            for (int v = 0; v < npixels; v++) {
                *dest = *src;
                dest++;
                src += 3;
            }
        }
        
        outb->incrementUsedSize(chunk_sz);
        write_nocopy(outb, "towriter_" + tostr(hostid));
        delete[] data_rgb;
        ckthisid++;
        if (ckthisid==chunks_per_host) {
            ckthisid=0;
            hostid++;
        }
        printf("\rrep %d of %d", t, output_tiles);
        fflush(stdout);
    }
    
    if ((f_dim = fopen(output_filename.c_str(), "w")) == NULL) {
        std::cerr << "ERROR: opening file"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    
    fprintf(f_dim,
            "type BGRplanar\n"
            "pixels_x %lld\n"
            "pixels_y %lld\n"
            "pixels_z 1\n"
            "chunks_x %d\n"
            "chunks_y %d\n"
            "chunks_z %d\n"
            "timestamp %s\n",
            ppm.pixels_x,
            ppm.pixels_y,
            1,
            output_tiles,
            1,
            dcmpi_get_time().c_str());
    fflush(f_dim);

    std::string x_string = "chunk_dimensions_x ";
    x_string += tostr(ppm.pixels_x);
    std::string y_string = "chunk_dimensions_y ";
    for (int yloop = 0; yloop < output_tiles; yloop++) {
        y_string = y_string + tostr(dims_y[yloop]) + " ";
    }
    dcmpi_string_trim_rear(y_string);
    fprintf(f_dim, 
            "%s\n%s\n",
            x_string.c_str(),
            y_string.c_str());

    for (uint u = 0; u < parts.size(); u++) {
        fprintf(f_dim, "%s\n", parts[u].c_str());
    }
    
    if (fclose(f_dim) != 0) {
        std::cerr << "ERROR: calling fclose()"
                  << " at " << __FILE__ << ":" << __LINE__
                  << std::endl << std::flush;
        exit(1);
    }
    double after = dcmpi_doubletime();
    return 0;
}
Example #16
0
DCBuffer* void_to_dcbuffer(void *buf, int bufSz) {
	DCBuffer * dcBuf = new DCBuffer(bufSz);
	memcpy(dcBuf->getPtr(), buf, bufSz);
	dcBuf->setUsedSize(bufSz);
	return dcBuf;
}
Example #17
0
int main(int argc, char * argv[])
{
    appname = argv[0];
    if (((argc-1) != 3)) {
        usage();
    }

    DCLayout layout;
    layout.use_filter_library("liball2allfilters.so");
    DCFilterInstance console("<console>", "console");
    layout.add(console);
    int lines = dcmpi_file_lines(argv[1]);
    int i, j;
    std::vector<DCFilterInstance*> filters;
    for (i = 0; i < lines; i++) {
        DCFilterInstance * f = new DCFilterInstance(
            "all2all", "all2all_" +  dcmpi_to_string(i));
        filters.push_back(f);
        layout.add(f);
        f->set_param("myid", dcmpi_to_string(i));
    }
    for (i = 0; i < lines; i++) {
        for (j = 0; j < lines; j++) {
            if (i != j) {
                layout.add_port(filters[i], dcmpi_to_string(j),
                                filters[j], "incoming");
            }
        }
        layout.add_port(filters[i], "ready", &console, "ready");
        layout.add_port(&console, "ready", filters[i], "ready");
    }
    layout.set_param_all("packet_size", argv[2]);
    layout.set_param_all("num_packets", argv[3]);
    layout.set_param_all("filter_count", dcmpi_to_string(lines));
    layout.set_exec_host_pool_file(argv[1]);

    DCFilter * c = layout.execute_start();

    // wait till all are ready
    cout << "waiting for all filters to be ready..." << flush;
    for (i = 0; i < lines; i++) {
        DCBuffer * b = c->read("ready");
        b->consume();
    }
    DCBuffer goBuf;
    for (i = 0; i < lines; i++) {
        c->write(&goBuf, "ready");
    }
    cout << "done\nall filters are ready, test is commencing\n";
    double tstart = dcmpi_doubletime();
    int rc = layout.execute_finish();
    double tstop = dcmpi_doubletime();
    double elapsed = tstop - tstart;
    double MB = ((dcmpi_csnum(argv[2]) * (dcmpi_csnum(argv[3])) *
                  (lines-1) * lines)
                 / (1024.0*1024.0));
    double MB_per_sec =  MB / elapsed;
    cout << "MB/sec " << MB_per_sec << " MB " << MB
         << " elapsed time " << elapsed
         << endl;
    return rc;
}
int ocvm_maximum_spanning_tree2::process(void)
{
    std::cout << "ocvm_maximum_spanning_tree2: running on "
              << dcmpi_get_hostname() << "\n";

    int subimage_width = get_param_as_int("subimage_width");
    int subimage_height = get_param_as_int("subimage_height");
    double subimage_overlap = Atof(get_param("subimage_overlap"));

    int i;
    int4 nXChunks, nYChunks;
    nXChunks = get_param_as_int("nXChunks");
    nYChunks = get_param_as_int("nYChunks");
    int4 score, x_displacement, y_displacement, x_ref, y_ref,
        x_subject, y_subject, left_to_right, diffX, diffY;
    std::cout << "ocvm_maximum_spanning_tree2: " << nXChunks
              << "x" << nYChunks << " image coming at me\n";

//     GlobalAligner ga = GlobalAligner(nXChunks, nYChunks, subimage_width, subimage_height, subimage_overlap/100.0);
    GlobalAligner ga = GlobalAligner(nXChunks, nYChunks);
    int num_incoming_packets =
        ((nXChunks-1) * nYChunks) +
        ((nYChunks-1) * nXChunks);
    std::cout << "ocvm_maximum_spanning_tree2: num_incoming_packets is "
              << num_incoming_packets << endl;

    for (i = 0; i < num_incoming_packets; i++) {
        DCBuffer * in = read("from_aligners");
        in->Extract(&score);
        in->Extract(&x_displacement);
        in->Extract(&y_displacement);
        in->Extract(&x_ref);
        in->Extract(&y_ref);
        in->Extract(&x_subject);
        in->Extract(&y_subject);
        in->Extract(&left_to_right);
        in->Extract(&diffX);
        in->Extract(&diffY);

//         std::cout << "maximum_spanning_tree2: next packet: "
//                   << score << " " << x_displacement << " " << y_displacement
//                   << " (" << x_ref << "," << y_ref
//                   << ") (" << x_subject  << "," << y_subject
//                   << ") " << left_to_right
//                   << " " << diffX << " " << diffY << "\n";
        
        int edge_index;
        if (left_to_right) {
//	   edge_index = (nYChunks - y_ref - 1)*(2*nXChunks - 1);
            edge_index = y_subject*(2*nXChunks - 1);
            if (y_subject == nYChunks-1) 
                edge_index = edge_index + x_ref;
            else
                edge_index = edge_index + 2*x_ref + 1;
            ga.init_edge(edge_index, x_displacement, y_displacement, score);
        }
        else {
            edge_index = y_ref*(2*nXChunks - 1) + 2*x_subject;
            ga.init_edge(edge_index, -1*x_displacement, -1*y_displacement, score);
        }
//	std::cout << "edge index= " << edge_index << std::endl;
        
        in->consume();
    }

    double before = dcmpi_doubletime();
    std::cout << "w: " << subimage_width << endl;
    std::cout << "h: " << subimage_height << endl;
    std::cout << "o: " << subimage_overlap << endl;
    ga.init_tiles((int8)(subimage_width * (1.0 - (subimage_overlap / 100.0))),
                  (int8)(subimage_height * (1.0 - (subimage_overlap / 100.0))));
//    ga.displayEdges();

//    std::cout << std::endl;
//    ga.displayOffsets();
//    std::cout << std::endl;
    ga.calculateDisplacements();
    std::cout << std::endl;
    ga.normalizeOffsets(); 
//    ga.displayOffsets();
    ga.finalizeOffsets();
//    ga.displayOffsets();
    std::cout << std::endl;

    double elapsed = dcmpi_doubletime() - before;
    DCBuffer *outb = new DCBuffer(8 + (((nXChunks * nYChunks * 2) + 2) * sizeof(int8)));
    std::cout << "spanning tree took this many seconds: " << elapsed <<endl;
    outb->Append(ga.maxX+subimage_width);
    outb->Append(ga.maxY+subimage_height);
    for (i = 0; i < nXChunks*nYChunks; i++) {
        outb->Append(ga.tiles[i]->offsetX);
        outb->Append(ga.tiles[i]->offsetY);
    }
    write_nocopy(outb, "to_console");

    std::cout << "f-mst.cpp: exiting\n";
    return 0;
}