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 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 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 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;
}