#include "Halide.h"
#include <tiramisu/utils.h>
#include <tiramisu/mpi_comm.h>
#include <cstdlib>
#include <iostream>
#include <complex>
#include "benchmarks.h"

#ifdef __cplusplus
extern "C" {
#endif

#include "tiramisu_generated_wrapper.h"

/* index functions */
int index_2d(int a, int b, int length2) {
   return b +length2*( a );
}
int index_3d(int a, int b, int c, int length2, int length3) {
   return c +length3*( b +length2*( a ));
}
int index_4d(int a, int b, int c, int d, int length2, int length3, int length4) {
   return d +length4*( c +length3*( b +length2*( a )));
}
int prop_index(int q, int t, int c1, int s1, int c2, int s2, int y, int x, int Nc, int Ns, int Vsrc, int Vsnk, int Nt) {
   return y +Vsrc*( x +Vsnk*( s1 +Ns*( c1 +Nc*( s2 +Ns*( c2 +Nc*( t +Nt* q ))))));
}

void tiramisu_make_nucleon_2pt(double* C_re,
    double* C_im,
    const double* B1_prop_re,
    const double* B1_prop_im,
    const int *src_color_weights_r1,
    const int *src_spin_weights_r1,
    const double *src_weights_r1,
    const int *src_color_weights_r2,
    const int *src_spin_weights_r2,
    const double *src_weights_r2,
    const double* src_psi_B1_re,
    const double* src_psi_B1_im,
    const double* snk_psi_B1_re,
    const double* snk_psi_B1_im,
    const int Nc,
    const int Ns,
    const int Vsrc,
    const int Vsnk,
    const int Lt,
    const int Nw,
    const int Nq,
    const int NsrcHex,
    const int NsnkHex,
    const int Nr)
{

   int q, t, iC, iS, jC, jS, y, x, m, n, k, wnum;

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (rank == 0) {
    long mega = 1024*1024;
    std::cout << "Array sizes" << std::endl;
    std::cout << "Prop:" <<  std::endl;
    std::cout << "	Max index size = " << Nq*Vsnk*Vsrc*Nc*Ns*Nc*Ns*Lt <<  std::endl;
    std::cout << "	Array size = " << Nq*Vsnk*Vsrc*Nc*Ns*Nc*Ns*Lt*sizeof(std::complex<double>)/mega << " Mega bytes" << std::endl;
    std::cout << "Q:" <<  std::endl;
    std::cout << "	Max index size = " << Vsnk*Vsrc*Nc*Ns*Nc*Ns <<  std::endl;
    std::cout << "	Array size = " << Vsnk*Vsrc*Nc*Ns*Nc*Ns*sizeof(std::complex<double>)/mega << " Mega bytes" <<  std::endl;

    long kilo = 1024;
    std::cout << "Blocal:" <<  std::endl;
    std::cout << "	Max index size = " << Vsnk*NsrcHex*Nc*Ns*Nc*Ns*Nc*Ns <<  std::endl;
    std::cout << "	Array size = " << Vsnk*NsrcHex*Nc*Ns*Nc*Ns*Nc*Ns*sizeof(std::complex<double>)/kilo << " kilo bytes" <<  std::endl;
    std::cout << std::endl;
    }

   // Halide buffers
   Halide::Buffer<double> b_C_r(NsnkHex, Nr, NsrcHex, Vsnk, Lt, "C_r");
   Halide::Buffer<double> b_C_i(NsnkHex, Nr, NsrcHex, Vsnk, Lt, "C_i");

   Halide::Buffer<int> b_src_color_weights(Nq, Nw, Nr, "src_color_weights");
   Halide::Buffer<int> b_src_spin_weights(Nq, Nw, Nr, "src_spin_weights");
   Halide::Buffer<double> b_src_weights(Nw, Nr, "src_weights");

   Halide::Buffer<int> b_snk_blocks(Nr, "snk_blocks");
   Halide::Buffer<int> b_snk_color_weights(Nq, Nw, Nr, "snk_color_weights");
   Halide::Buffer<int> b_snk_spin_weights(Nq, Nw, Nr, "snk_spin_weights");
   Halide::Buffer<double> b_snk_weights(Nw, Nr, "snk_weights");

    // prop
    Halide::Buffer<double> b_B1_prop_r((double *)B1_prop_re, {Vsrc, Vsnk, Ns, Nc, Ns, Nc, Lt, Nq});
    Halide::Buffer<double> b_B1_prop_i((double *)B1_prop_im, {Vsrc, Vsnk, Ns, Nc, Ns, Nc, Lt, Nq});

    if (rank == 0)
   printf("prop elem %4.9f \n", b_B1_prop_r(0,0,0,0,0,0,0,0));

    // psi
    Halide::Buffer<double> b_B1_src_psi_r((double *)src_psi_B1_re, {NsrcHex, Vsrc});
    Halide::Buffer<double> b_B1_src_psi_i((double *)src_psi_B1_im, {NsrcHex, Vsrc});
    Halide::Buffer<double> b_B1_snk_psi_r((double *)snk_psi_B1_re, {NsnkHex, Vsnk});
    Halide::Buffer<double> b_B1_snk_psi_i((double *)snk_psi_B1_im, {NsnkHex, Vsnk});

   // Weights
   int* snk_color_weights_r1 = (int *) malloc(Nw * Nq * sizeof (int));
   int* snk_color_weights_r2 = (int *) malloc(Nw * Nq * sizeof (int));
   int* snk_spin_weights_r1 = (int *) malloc(Nw * Nq * sizeof (int));
   int* snk_spin_weights_r2 = (int *) malloc(Nw * Nq * sizeof (int));
   for (int nB1=0; nB1<Nw; nB1++) {
         b_src_weights(nB1, 0) = src_weights_r1[nB1];
         b_src_weights(nB1, 1) = src_weights_r2[nB1];
         b_snk_weights(nB1, 0) = src_weights_r1[nB1];
         b_snk_weights(nB1, 1) = src_weights_r2[nB1];
         for (int nq=0; nq<Nq; nq++) {
            // G1g_r1
            snk_color_weights_r1[index_2d(nB1,nq ,Nq)] = src_color_weights_r1[index_2d(nB1,nq ,Nq)];
            snk_spin_weights_r1[index_2d(nB1,nq ,Nq)] = src_spin_weights_r1[index_2d(nB1,nq ,Nq)];
            // G1g_r2
            snk_color_weights_r2[index_2d(nB1,nq ,Nq)] = src_color_weights_r2[index_2d(nB1,nq ,Nq)];
            snk_spin_weights_r2[index_2d(nB1,nq ,Nq)] = src_spin_weights_r2[index_2d(nB1,nq ,Nq)];
         }
   }
   b_snk_blocks(0) = 1;
   b_snk_blocks(1) = 2;
      for (int wnum=0; wnum< Nw; wnum++) {
         b_src_color_weights(0, wnum, 0) = snk_color_weights_r1[index_2d(wnum,0 ,Nq)];
         b_src_spin_weights(0, wnum, 0) = snk_spin_weights_r1[index_2d(wnum,0 ,Nq)];
         b_src_color_weights(1, wnum, 0) = snk_color_weights_r1[index_2d(wnum,1 ,Nq)];
         b_src_spin_weights(1, wnum, 0) = snk_spin_weights_r1[index_2d(wnum,1 ,Nq)];
         b_src_color_weights(2, wnum, 0) = snk_color_weights_r1[index_2d(wnum,2 ,Nq)];
         b_src_spin_weights(2, wnum, 0) = snk_spin_weights_r1[index_2d(wnum,2 ,Nq)];

         b_src_color_weights(0, wnum, 1) = snk_color_weights_r2[index_2d(wnum,0 ,Nq)];
         b_src_spin_weights(0, wnum, 1) = snk_spin_weights_r2[index_2d(wnum,0 ,Nq)];
         b_src_color_weights(1, wnum, 1) = snk_color_weights_r2[index_2d(wnum,1 ,Nq)];
         b_src_spin_weights(1, wnum, 1) = snk_spin_weights_r2[index_2d(wnum,1 ,Nq)];
         b_src_color_weights(2, wnum, 1) = snk_color_weights_r2[index_2d(wnum,2 ,Nq)];
         b_src_spin_weights(2, wnum, 1) = snk_spin_weights_r2[index_2d(wnum,2 ,Nq)];

         b_snk_color_weights(0, wnum, 0) = snk_color_weights_r1[index_2d(wnum,0 ,Nq)];
         b_snk_spin_weights(0, wnum, 0) = snk_spin_weights_r1[index_2d(wnum,0 ,Nq)];
         b_snk_color_weights(1, wnum, 0) = snk_color_weights_r1[index_2d(wnum,1 ,Nq)];
         b_snk_spin_weights(1, wnum, 0) = snk_spin_weights_r1[index_2d(wnum,1 ,Nq)];
         b_snk_color_weights(2, wnum, 0) = snk_color_weights_r1[index_2d(wnum,2 ,Nq)];
         b_snk_spin_weights(2, wnum, 0) = snk_spin_weights_r1[index_2d(wnum,2 ,Nq)];

         b_snk_color_weights(0, wnum, 1) = snk_color_weights_r2[index_2d(wnum,0 ,Nq)];
         b_snk_spin_weights(0, wnum, 1) = snk_spin_weights_r2[index_2d(wnum,0 ,Nq)];
         b_snk_color_weights(1, wnum, 1) = snk_color_weights_r2[index_2d(wnum,1 ,Nq)];
         b_snk_spin_weights(1, wnum, 1) = snk_spin_weights_r2[index_2d(wnum,1 ,Nq)];
         b_snk_color_weights(2, wnum, 1) = snk_color_weights_r2[index_2d(wnum,2 ,Nq)];
         b_snk_spin_weights(2, wnum, 1) = snk_spin_weights_r2[index_2d(wnum,2 ,Nq)];
      }

   for (int b=0; b<Nr; b++)
      for (int m=0; m<NsrcHex; m++)
         for (int n=0; n<NsnkHex; n++)
               for (int x=0; x<Vsnk; x++) {
                  b_C_r(n,b,m,x,t) = 0.0;
                  b_C_i(n,b,m,x,t) = 0.0;
            }

   if (rank == 0) {
   printf("prop 1 %4.16f + I %4.16f \n", b_B1_prop_r(0,0,0,0,0,0,0,0), b_B1_prop_i(0,0,0,0,0,0,0,0));
   printf("psi src 1 %4.16f + I %4.16f \n", b_B1_src_psi_r(0,0), b_B1_src_psi_i(0,0));
   printf("psi snk %4.16f + I %4.16f \n", b_B1_snk_psi_r(0,0,0), b_B1_snk_psi_i(0,0,0));
   printf("weights snk %4.16f \n", b_snk_weights(0,0));
   }
   tiramisu_make_fused_baryon_blocks_correlator(
				    b_C_r.raw_buffer(),
				    b_C_i.raw_buffer(),
				    b_B1_prop_r.raw_buffer(),
				    b_B1_prop_i.raw_buffer(),
                b_B1_src_psi_r.raw_buffer(),
                b_B1_src_psi_i.raw_buffer(),
                b_B1_snk_psi_r.raw_buffer(),
                b_B1_snk_psi_i.raw_buffer(),
				    b_src_color_weights.raw_buffer(),
				    b_src_spin_weights.raw_buffer(),
				    b_src_weights.raw_buffer(),
				    b_snk_blocks.raw_buffer(),
				    b_snk_color_weights.raw_buffer(),
				    b_snk_spin_weights.raw_buffer(),
				    b_snk_weights.raw_buffer());

    // symmetrize and such
      for (int m=0; m<NsrcHex; m++)
         for (int n=0; n<NsnkHex; n++)
            for (int t=0; t<Lt; t++)  {
               double number0r;
               double number0i;
               double number1r;
               double number1i;
               double this_number0r = b_C_r(n,0,m,rank,t);
               double this_number0i = b_C_i(n,0,m,rank,t);
               double this_number1r = b_C_r(n,1,m,rank,t);
               double this_number1i = b_C_i(n,1,m,rank,t);
               MPI_Allreduce(&this_number0r, &number0r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number0i, &number0i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1r, &number1r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1i, &number1i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Barrier(MPI_COMM_WORLD);
                  C_re[index_4d(0,m,n,t, NsrcHex,NsnkHex,Lt)] = number0r;
                  C_re[index_4d(1,m,n,t, NsrcHex,NsnkHex,Lt)] = number1r;
                  C_im[index_4d(0,m,n,t, NsrcHex,NsnkHex,Lt)] = number0i;
                  C_im[index_4d(1,m,n,t, NsrcHex,NsnkHex,Lt)] = number1i;
               }

   if (rank == 0) {
   for (int b=0; b<2; b++) {
      printf("\n");
      for (int m=0; m<NsrcHex; m++)
         for (int n=0; n<NsnkHex; n++)
            for (int t=0; t<Lt; t++) {
                  printf("r=%d, m=%d, n=%d, t=%d: %4.9e + I (%4.9e) \n", b, m, n, t, C_re[index_4d(b,m,n,t, NsrcHex,NsnkHex,Lt)],  C_im[index_4d(b,m,n,t, NsrcHex,NsnkHex,Lt)]);
            }
   }
   }

   free(snk_color_weights_r1);
   free(snk_color_weights_r2);
   free(snk_spin_weights_r1);
   free(snk_spin_weights_r2);
}

void tiramisu_make_two_nucleon_2pt(double* C_re,
    double* C_im,
    const double* B1_prop_re,
    const double* B1_prop_im,
    const double* B2_prop_re,
    const double* B2_prop_im,
    const int *src_color_weights_r1,
    const int *src_spin_weights_r1,
    const double *src_weights_r1,
    const int *src_color_weights_r2,
    const int *src_spin_weights_r2,
    const double *src_weights_r2,
    const int *hex_snk_color_weights_A1,
    const int *hex_snk_spin_weights_A1,
    const double *hex_snk_weights_A1,
    const int *hex_snk_color_weights_T1_r1,
    const int *hex_snk_spin_weights_T1_r1,
    const double *hex_snk_weights_T1_r1,
    const int *hex_snk_color_weights_T1_r2,
    const int *hex_snk_spin_weights_T1_r2,
    const double *hex_snk_weights_T1_r2,
    const int *hex_snk_color_weights_T1_r3,
    const int *hex_snk_spin_weights_T1_r3,
    const double *hex_snk_weights_T1_r3,
    const int* perms,
    const int* sigs,
    const double* src_psi_B1_re,
    const double* src_psi_B1_im,
    const double* src_psi_B2_re,
    const double* src_psi_B2_im,
    const double* snk_psi_re,
    const double* snk_psi_im,
    const double* snk_psi_B1_re,
    const double* snk_psi_B1_im,
    const double* snk_psi_B2_re,
    const double* snk_psi_B2_im,
    const double* hex_src_psi_re,
    const double* hex_src_psi_im,
    const double* hex_snk_psi_re,
    const double* hex_snk_psi_im,
    const int Nc,
    const int Ns,
    const int Vsrc,
    const int Vsnk,
    const int Lt,
    const int Nw,
    const int Nq,
    const int Nsrc,
    const int Nsnk,
    const int NsrcHex,
    const int NsnkHex,
    const int Nperms,
    const int Nr,
    const int Nw2Hex,
    const int Nb,
    const int NEntangled)
{

   int q, t, iC, iS, jC, jS, y, x, x1, x2, m, n, k, wnum, nperm, b;
   int Nw2 = Nw*Nw;

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

   if (rank == 0) {
    long mega = 1024*1024;
    long kilo = 1024;
    std::cout << "Array sizes" << std::endl;
    std::cout << "Prop:" <<  std::endl;
    std::cout << "	Max index size = " << Nq*Vsnk*Vsrc*Nc*Ns*Nc*Ns*Lt <<  std::endl;
    std::cout << "	Array size = " << Nq*Vsnk*Vsrc*Nc*Ns*Nc*Ns*Lt*sizeof(std::complex<double>)/mega << " Mega bytes" << std::endl;
    std::cout << "Blocal, Bsingle, Bdouble:" <<  std::endl;
    std::cout << "	Max index size = " << Nsrc*Nc*Ns*Nc*Ns*Nc*Ns <<  std::endl;
    std::cout << "	Array size = " << Nsrc*Nc*Ns*Nc*Ns*Nc*Ns*sizeof(std::complex<double>)/kilo << " kilo bytes" <<  std::endl;
    std::cout << std::endl;
  }

   // Halide buffers
   Halide::Buffer<double> b_C_r(Nsnk+NsnkHex, Nr, Nsrc+NsrcHex, Vsnk, Lt, "C_r");
   Halide::Buffer<double> b_C_i(Nsnk+NsnkHex, Nr, Nsrc+NsrcHex, Vsnk, Lt, "C_i");
   //Halide::Buffer<double> b_C_r(Nsnk+NsnkHex, Nr, Nsrc+NsrcHex, Lt, "C_r");
   //Halide::Buffer<double> b_C_i(Nsnk+NsnkHex, Nr, Nsrc+NsrcHex, Lt, "C_i");

   Halide::Buffer<int> b_src_color_weights(Nq, Nw, Nr, "src_color_weights");
   Halide::Buffer<int> b_src_spin_weights(Nq, Nw, Nr, "src_spin_weights");
   Halide::Buffer<double> b_src_weights(Nw, Nr, "src_weights");

   Halide::Buffer<int> b_snk_blocks(2, Nr, "snk_blocks");
   Halide::Buffer<int> b_snk_b(2, Nq, Nperms, "snk_b");
   Halide::Buffer<int> b_snk_color_weights(2, Nq, Nw2, Nperms, Nr, "snk_color_weights");
   Halide::Buffer<int> b_snk_spin_weights(2, Nq, Nw2, Nperms, Nr, "snk_spin_weights");
   Halide::Buffer<double> b_snk_weights(Nw2, Nr, "snk_weights");
   Halide::Buffer<int> b_hex_snk_color_weights(2, Nq, Nw2Hex, Nperms, Nr, "hex_snk_color_weights");
   Halide::Buffer<int> b_hex_snk_spin_weights(2, Nq, Nw2Hex, Nperms, Nr, "hex_snk_spin_weights");
   Halide::Buffer<double> b_hex_snk_weights(Nw2Hex, Nr, "hex_snk_weights");

    // prop
    Halide::Buffer<double> b_B1_prop_r((double *)B1_prop_re, {Vsrc, Vsnk, Ns, Nc, Ns, Nc, Lt, Nq});
    Halide::Buffer<double> b_B1_prop_i((double *)B1_prop_im, {Vsrc, Vsnk, Ns, Nc, Ns, Nc, Lt, Nq});
    Halide::Buffer<double> b_B2_prop_r((double *)B2_prop_re, {Vsrc, Vsnk, Ns, Nc, Ns, Nc, Lt, Nq});
    Halide::Buffer<double> b_B2_prop_i((double *)B2_prop_im, {Vsrc, Vsnk, Ns, Nc, Ns, Nc, Lt, Nq});

    // psi
    Halide::Buffer<double> b_B1_src_psi_r((double *)src_psi_B1_re, {Nsrc, Vsrc});
    Halide::Buffer<double> b_B1_src_psi_i((double *)src_psi_B1_im, {Nsrc, Vsrc});
    Halide::Buffer<double> b_B2_src_psi_r((double *)src_psi_B2_re, {Nsrc, Vsrc});
    Halide::Buffer<double> b_B2_src_psi_i((double *)src_psi_B2_im, {Nsrc, Vsrc});
    Halide::Buffer<double> b_B1_snk_psi_r((double *)snk_psi_B1_re, {Nsnk, Vsnk});
    Halide::Buffer<double> b_B1_snk_psi_i((double *)snk_psi_B1_im, {Nsnk, Vsnk});
    Halide::Buffer<double> b_B2_snk_psi_r((double *)snk_psi_B2_re, {Nsnk, Vsnk});
    Halide::Buffer<double> b_B2_snk_psi_i((double *)snk_psi_B2_im, {Nsnk, Vsnk});
    Halide::Buffer<double> b_hex_src_psi_r((double *)hex_src_psi_re, {NsrcHex, Vsrc});
    Halide::Buffer<double> b_hex_src_psi_i((double *)hex_src_psi_im, {NsrcHex, Vsrc});
    Halide::Buffer<double> b_hex_snk_psi_r((double *)hex_snk_psi_re, {NsnkHex, Vsnk});
    Halide::Buffer<double> b_hex_snk_psi_i((double *)hex_snk_psi_im, {NsnkHex, Vsnk});
    Halide::Buffer<double> b_snk_psi_r((double *)snk_psi_re, {NEntangled, Vsnk, Vsnk});
    Halide::Buffer<double> b_snk_psi_i((double *)snk_psi_im, {NEntangled, Vsnk, Vsnk});

   Halide::Buffer<int> b_sigs((int *)sigs, {Nperms});

   // Weights
      for (int wnum=0; wnum< Nw; wnum++) {
         b_src_weights(wnum, 0) = src_weights_r1[wnum];
         b_src_weights(wnum, 1) = src_weights_r2[wnum];

         b_src_color_weights(0, wnum, 0) = src_color_weights_r1[index_2d(wnum,0 ,Nq)];
         b_src_spin_weights(0, wnum, 0) = src_spin_weights_r1[index_2d(wnum,0 ,Nq)];
         b_src_color_weights(1, wnum, 0) = src_color_weights_r1[index_2d(wnum,1 ,Nq)];
         b_src_spin_weights(1, wnum, 0) = src_spin_weights_r1[index_2d(wnum,1 ,Nq)];
         b_src_color_weights(2, wnum, 0) = src_color_weights_r1[index_2d(wnum,2 ,Nq)];
         b_src_spin_weights(2, wnum, 0) = src_spin_weights_r1[index_2d(wnum,2 ,Nq)];

         b_src_color_weights(0, wnum, 1) = src_color_weights_r2[index_2d(wnum,0 ,Nq)];
         b_src_spin_weights(0, wnum, 1) = src_spin_weights_r2[index_2d(wnum,0 ,Nq)];
         b_src_color_weights(1, wnum, 1) = src_color_weights_r2[index_2d(wnum,1 ,Nq)];
         b_src_spin_weights(1, wnum, 1) = src_spin_weights_r2[index_2d(wnum,1 ,Nq)];
         b_src_color_weights(2, wnum, 1) = src_color_weights_r2[index_2d(wnum,2 ,Nq)];
         b_src_spin_weights(2, wnum, 1) = src_spin_weights_r2[index_2d(wnum,2 ,Nq)];
      }
   int* snk_color_weights_1 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_color_weights_2 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_color_weights_r1 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_color_weights_r2_1 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_color_weights_r2_2 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_color_weights_r3 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_spin_weights_1 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_spin_weights_2 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_spin_weights_r1 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_spin_weights_r2_1 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_spin_weights_r2_2 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));
   int* snk_spin_weights_r3 = (int *) malloc(2 * Nw2 * Nq * sizeof (int));

   for (int nB1=0; nB1<Nw; nB1++) {
      for (int nB2=0; nB2<Nw; nB2++) {
         b_snk_weights(nB1+Nw*nB2, 0) = src_weights_r1[nB1]*src_weights_r1[nB2];
         b_snk_weights(nB1+Nw*nB2, 1) = src_weights_r2[nB1]*src_weights_r2[nB2];
         b_snk_weights(nB1+Nw*nB2, 2) = 1.0/sqrt(2) * src_weights_r1[nB1]*src_weights_r2[nB2];
         b_snk_weights(nB1+Nw*nB2, 3) = 1.0/sqrt(2) * src_weights_r1[nB1]*src_weights_r2[nB2];
         b_snk_weights(nB1+Nw*nB2, 4) = 1.0/sqrt(2) * src_weights_r2[nB1]*src_weights_r1[nB2];
         b_snk_weights(nB1+Nw*nB2, 5) = 1.0/sqrt(2) * src_weights_r2[nB1]*src_weights_r1[nB2];
         for (int nq=0; nq<Nq; nq++) {
            // T1g_r1
            snk_color_weights_r1[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r1[index_2d(nB1,nq ,Nq)];
            snk_spin_weights_r1[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r1[index_2d(nB1,nq ,Nq)];
            snk_color_weights_r1[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r1[index_2d(nB2,nq ,Nq)];
            snk_spin_weights_r1[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r1[index_2d(nB2,nq ,Nq)];
            // T1g_r2
            snk_color_weights_r2_1[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r1[index_2d(nB1,nq ,Nq)];
            snk_spin_weights_r2_1[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r1[index_2d(nB1,nq ,Nq)];
            snk_color_weights_r2_1[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r2[index_2d(nB2,nq ,Nq)];
            snk_spin_weights_r2_1[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r2[index_2d(nB2,nq ,Nq)];
            snk_color_weights_r2_2[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r2[index_2d(nB1,nq ,Nq)];
            snk_spin_weights_r2_2[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r2[index_2d(nB1,nq ,Nq)];
            snk_color_weights_r2_2[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r1[index_2d(nB2,nq ,Nq)];
            snk_spin_weights_r2_2[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r1[index_2d(nB2,nq ,Nq)];
            // T1g_r3
            snk_color_weights_r3[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r2[index_2d(nB1,nq ,Nq)];
            snk_spin_weights_r3[index_3d(0,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r2[index_2d(nB1,nq ,Nq)];
            snk_color_weights_r3[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_color_weights_r2[index_2d(nB2,nq ,Nq)];
            snk_spin_weights_r3[index_3d(1,nB1+Nw*nB2,nq ,Nw2,Nq)] = src_spin_weights_r2[index_2d(nB2,nq ,Nq)];
         }
      }
   }
   b_snk_blocks(0,0) = 1;
   b_snk_blocks(1,0) = 1;
   b_snk_blocks(0,1) = 2;
   b_snk_blocks(1,1) = 2;
   b_snk_blocks(0,2) = 1;
   b_snk_blocks(1,2) = 2;
   b_snk_blocks(0,3) = 2;
   b_snk_blocks(1,3) = 1;
   b_snk_blocks(0,4) = 1;
   b_snk_blocks(1,4) = 2;
   b_snk_blocks(0,5) = 2;
   b_snk_blocks(1,5) = 1;
   int snk_1_nq[Nb];
   int snk_2_nq[Nb];
   int snk_3_nq[Nb];
   int snk_1_b[Nb];
   int snk_2_b[Nb];
   int snk_3_b[Nb];
   int snk_1[Nb];
   int snk_2[Nb];
   int snk_3[Nb];
   for (int nperm=0; nperm<Nperms; nperm++) {
      for (int b=0; b<Nb; b++) {
         snk_1[b] = perms[index_2d(nperm,Nq*b+0 ,2*Nq)] - 1;
         snk_2[b] = perms[index_2d(nperm,Nq*b+1 ,2*Nq)] - 1;
         snk_3[b] = perms[index_2d(nperm,Nq*b+2 ,2*Nq)] - 1;
         snk_1_b[b] = (snk_1[b] - snk_1[b] % Nq) / Nq;
         snk_2_b[b] = (snk_2[b] - snk_2[b] % Nq) / Nq;
         snk_3_b[b] = (snk_3[b] - snk_3[b] % Nq) / Nq;
         snk_1_nq[b] = snk_1[b] % Nq;
         snk_2_nq[b] = snk_2[b] % Nq;
         snk_3_nq[b] = snk_3[b] % Nq;
         b_snk_b(b, 0, nperm) = snk_1_b[b];
         b_snk_b(b, 1, nperm) = snk_2_b[b];
         b_snk_b(b, 2, nperm) = snk_3_b[b];
      }
      for (int wnum=0; wnum< Nw2; wnum++) {
         b_snk_color_weights(0, 0, wnum, nperm, 0) = snk_color_weights_r1[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 0, wnum, nperm, 0) = snk_spin_weights_r1[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 1, wnum, nperm, 0) = snk_color_weights_r1[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 1, wnum, nperm, 0) = snk_spin_weights_r1[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 2, wnum, nperm, 0) = snk_color_weights_r1[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 2, wnum, nperm, 0) = snk_spin_weights_r1[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(1, 0, wnum, nperm, 0) = snk_color_weights_r1[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 0, wnum, nperm, 0) = snk_spin_weights_r1[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 1, wnum, nperm, 0) = snk_color_weights_r1[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 1, wnum, nperm, 0) = snk_spin_weights_r1[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 2, wnum, nperm, 0) = snk_color_weights_r1[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 2, wnum, nperm, 0) = snk_spin_weights_r1[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];

         b_snk_color_weights(0, 0, wnum, nperm, 1) = snk_color_weights_r3[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 0, wnum, nperm, 1) = snk_spin_weights_r3[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 1, wnum, nperm, 1) = snk_color_weights_r3[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 1, wnum, nperm, 1) = snk_spin_weights_r3[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 2, wnum, nperm, 1) = snk_color_weights_r3[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 2, wnum, nperm, 1) = snk_spin_weights_r3[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(1, 0, wnum, nperm, 1) = snk_color_weights_r3[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 0, wnum, nperm, 1) = snk_spin_weights_r3[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 1, wnum, nperm, 1) = snk_color_weights_r3[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 1, wnum, nperm, 1) = snk_spin_weights_r3[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 2, wnum, nperm, 1) = snk_color_weights_r3[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 2, wnum, nperm, 1) = snk_spin_weights_r3[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];

         b_snk_color_weights(0, 0, wnum, nperm, 2) = snk_color_weights_r2_1[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 0, wnum, nperm, 2) = snk_spin_weights_r2_1[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 1, wnum, nperm, 2) = snk_color_weights_r2_1[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 1, wnum, nperm, 2) = snk_spin_weights_r2_1[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 2, wnum, nperm, 2) = snk_color_weights_r2_1[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 2, wnum, nperm, 2) = snk_spin_weights_r2_1[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(1, 0, wnum, nperm, 2) = snk_color_weights_r2_1[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 0, wnum, nperm, 2) = snk_spin_weights_r2_1[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 1, wnum, nperm, 2) = snk_color_weights_r2_1[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 1, wnum, nperm, 2) = snk_spin_weights_r2_1[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 2, wnum, nperm, 2) = snk_color_weights_r2_1[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 2, wnum, nperm, 2) = snk_spin_weights_r2_1[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];

         b_snk_color_weights(0, 0, wnum, nperm, 3) = snk_color_weights_r2_1[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 0, wnum, nperm, 3) = snk_spin_weights_r2_1[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 1, wnum, nperm, 3) = snk_color_weights_r2_1[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 1, wnum, nperm, 3) = snk_spin_weights_r2_1[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 2, wnum, nperm, 3) = snk_color_weights_r2_1[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 2, wnum, nperm, 3) = snk_spin_weights_r2_1[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(1, 0, wnum, nperm, 3) = snk_color_weights_r2_1[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 0, wnum, nperm, 3) = snk_spin_weights_r2_1[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 1, wnum, nperm, 3) = snk_color_weights_r2_1[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 1, wnum, nperm, 3) = snk_spin_weights_r2_1[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 2, wnum, nperm, 3) = snk_color_weights_r2_1[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 2, wnum, nperm, 3) = snk_spin_weights_r2_1[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];

         b_snk_color_weights(0, 0, wnum, nperm, 4) = snk_color_weights_r2_2[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 0, wnum, nperm, 4) = snk_spin_weights_r2_2[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 1, wnum, nperm, 4) = snk_color_weights_r2_2[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 1, wnum, nperm, 4) = snk_spin_weights_r2_2[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 2, wnum, nperm, 4) = snk_color_weights_r2_2[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 2, wnum, nperm, 4) = snk_spin_weights_r2_2[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(1, 0, wnum, nperm, 4) = snk_color_weights_r2_2[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 0, wnum, nperm, 4) = snk_spin_weights_r2_2[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 1, wnum, nperm, 4) = snk_color_weights_r2_2[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 1, wnum, nperm, 4) = snk_spin_weights_r2_2[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 2, wnum, nperm, 4) = snk_color_weights_r2_2[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 2, wnum, nperm, 4) = snk_spin_weights_r2_2[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];

         b_snk_color_weights(0, 0, wnum, nperm, 5) = snk_color_weights_r2_2[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 0, wnum, nperm, 5) = snk_spin_weights_r2_2[index_3d(snk_1_b[0],wnum,snk_1_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 1, wnum, nperm, 5) = snk_color_weights_r2_2[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 1, wnum, nperm, 5) = snk_spin_weights_r2_2[index_3d(snk_2_b[0],wnum,snk_2_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(0, 2, wnum, nperm, 5) = snk_color_weights_r2_2[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_spin_weights(0, 2, wnum, nperm, 5) = snk_spin_weights_r2_2[index_3d(snk_3_b[0],wnum,snk_3_nq[0] ,Nw2,Nq)];
         b_snk_color_weights(1, 0, wnum, nperm, 5) = snk_color_weights_r2_2[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 0, wnum, nperm, 5) = snk_spin_weights_r2_2[index_3d(snk_1_b[1],wnum,snk_1_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 1, wnum, nperm, 5) = snk_color_weights_r2_2[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 1, wnum, nperm, 5) = snk_spin_weights_r2_2[index_3d(snk_2_b[1],wnum,snk_2_nq[1] ,Nw2,Nq)];
         b_snk_color_weights(1, 2, wnum, nperm, 5) = snk_color_weights_r2_2[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
         b_snk_spin_weights(1, 2, wnum, nperm, 5) = snk_spin_weights_r2_2[index_3d(snk_3_b[1],wnum,snk_3_nq[1] ,Nw2,Nq)];
      }
      for (int wnum=0; wnum< Nw2Hex; wnum++) {
         for (int q=0; q < 2*Nq; q++) {
            b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 0) = hex_snk_color_weights_T1_r1[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 0) = hex_snk_spin_weights_T1_r1[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 1) = hex_snk_color_weights_T1_r3[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 1) = hex_snk_spin_weights_T1_r3[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 2) = hex_snk_color_weights_T1_r2[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 2) = hex_snk_spin_weights_T1_r2[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 3) = hex_snk_color_weights_T1_r2[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 3) = hex_snk_spin_weights_T1_r2[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 4) = hex_snk_color_weights_A1[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 4) = hex_snk_spin_weights_A1[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 5) = hex_snk_color_weights_A1[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 5) = hex_snk_spin_weights_A1[index_2d(wnum,perms[index_2d(nperm,q ,2*Nq)]-1 ,2*Nq)];
            //printf("hex color weight (%d, %d) r=4: %d \n", wnum, q, b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 4));
            //printf("hex color weight (%d, %d) r=4: %d \n", wnum, q, b_hex_snk_color_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 4));
            //printf("hex spin weight (%d, %d) r=4: %d \n", wnum, q, b_hex_snk_spin_weights((q-(q%Nq))/Nq, q%Nq, wnum, nperm, 4));
         }
      }
   }
   for (int wnum=0; wnum< Nw2Hex; wnum++) {
      b_hex_snk_weights(wnum, 0) = hex_snk_weights_T1_r1[wnum];
      b_hex_snk_weights(wnum, 1) = hex_snk_weights_T1_r3[wnum];
      b_hex_snk_weights(wnum, 2) = hex_snk_weights_T1_r2[wnum];
      b_hex_snk_weights(wnum, 3) = hex_snk_weights_T1_r2[wnum];
      b_hex_snk_weights(wnum, 4) = hex_snk_weights_A1[wnum];
      b_hex_snk_weights(wnum, 5) = hex_snk_weights_A1[wnum];
      //printf("hex weight %d r=4: %4.9f \n", wnum, b_hex_snk_weights(wnum, 4));
   }

   for (int b=0; b<Nr; b++)
      for (int m=0; m<Nsrc+NsrcHex; m++)
         for (int n=0; n<Nsnk+NsnkHex; n++)
            for (int t=0; t<Lt; t++) //{
               //b_C_r(n,b,m,t) = 0.0;
               //b_C_i(n,b,m,t) = 0.0;
               for (int x=0; x<Vsnk; x++) {
                  b_C_r(n,b,m,x,t) = 0.0;
                  b_C_i(n,b,m,x,t) = 0.0;
            }

   if (rank == 0) {
   printf("prop 1 %4.16f + I %4.16f \n", b_B1_prop_r(0,0,0,0,0,0,0,0), b_B1_prop_i(0,0,0,0,0,0,0,0));
   printf("prop 2 %4.16f + I %4.16f \n", b_B2_prop_r(0,0,0,0,0,0,0,0), b_B2_prop_i(0,0,0,0,0,0,0,0));
   printf("psi src 1 %4.16f + I %4.16f \n", b_B1_src_psi_r(0,0), b_B1_src_psi_i(0,0));
   printf("psi src 2 %4.16f + I %4.16f \n", b_B2_src_psi_r(0,0), b_B2_src_psi_i(0,0));
   printf("psi snk %4.16f + I %4.16f \n", b_snk_psi_r(0,0,0), b_snk_psi_i(0,0,0));
   printf("weights snk %4.9e \n", b_snk_weights(0,0));
   printf("sigs %d \n", b_sigs(0));
   }
   tiramisu_make_fused_dibaryon_blocks_correlator(
				    b_C_r.raw_buffer(),
				    b_C_i.raw_buffer(),
				    b_B1_prop_r.raw_buffer(),
				    b_B1_prop_i.raw_buffer(),
				    b_B2_prop_r.raw_buffer(),
				    b_B2_prop_i.raw_buffer(),
                b_B1_src_psi_r.raw_buffer(),
                b_B1_src_psi_i.raw_buffer(),
                b_B2_src_psi_r.raw_buffer(),
                b_B2_src_psi_i.raw_buffer(),
                b_B1_snk_psi_r.raw_buffer(),
                b_B1_snk_psi_i.raw_buffer(),
                b_B2_snk_psi_r.raw_buffer(),
                b_B2_snk_psi_i.raw_buffer(),
                b_hex_src_psi_r.raw_buffer(),
                b_hex_src_psi_i.raw_buffer(),
                b_hex_snk_psi_r.raw_buffer(),
                b_hex_snk_psi_i.raw_buffer(),
                b_snk_psi_r.raw_buffer(),
                b_snk_psi_i.raw_buffer(),
				    b_snk_blocks.raw_buffer(),
				    b_sigs.raw_buffer(),
				 b_src_color_weights.raw_buffer(),
				 b_src_spin_weights.raw_buffer(),
				 b_src_weights.raw_buffer(),
				    b_snk_b.raw_buffer(),
				    b_snk_color_weights.raw_buffer(),
				    b_snk_spin_weights.raw_buffer(),
				    b_snk_weights.raw_buffer(),
                                    b_hex_snk_color_weights.raw_buffer(),
                                    b_hex_snk_spin_weights.raw_buffer(),
                                    b_hex_snk_weights.raw_buffer());

   if (rank == 0) {
   /*printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,0,0,0), b_C_i(0,0,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,1,0,0), b_C_i(0,1,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,2,0,0), b_C_i(0,2,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,3,0,0), b_C_i(0,3,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,4,0,0), b_C_i(0,4,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,5,0,0), b_C_i(0,5,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,0,Nsrc,0), b_C_i(0,0,Nsrc,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,1,Nsrc,0), b_C_i(0,1,Nsrc,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,2,Nsrc,0), b_C_i(0,2,Nsrc,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,3,Nsrc,0), b_C_i(0,3,Nsrc,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,4,Nsrc,0), b_C_i(0,4,Nsrc,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,5,Nsrc,0), b_C_i(0,5,Nsrc,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,0,Nsrc,0), b_C_i(Nsnk,0,Nsrc,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,1,Nsrc,0), b_C_i(Nsnk,1,Nsrc,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,2,Nsrc,0), b_C_i(Nsnk,2,Nsrc,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,3,Nsrc,0), b_C_i(Nsnk,3,Nsrc,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,4,Nsrc,0), b_C_i(Nsnk,4,Nsrc,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,5,Nsrc,0), b_C_i(Nsnk,5,Nsrc,0) ); */
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,0,0,0,0), b_C_i(0,0,0,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,1,0,0,0), b_C_i(0,1,0,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,2,0,0,0), b_C_i(0,2,0,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,3,0,0,0), b_C_i(0,3,0,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,4,0,0,0), b_C_i(0,4,0,0,0) );
   printf("BB-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,5,0,0,0), b_C_i(0,5,0,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,0,Nsrc,0,0), b_C_i(0,0,Nsrc,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,1,Nsrc,0,0), b_C_i(0,1,Nsrc,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,2,Nsrc,0,0), b_C_i(0,2,Nsrc,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,3,Nsrc,0,0), b_C_i(0,3,Nsrc,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,4,Nsrc,0,0), b_C_i(0,4,Nsrc,0,0) );
   printf("H-BB non-zero? %4.9e + I %4.9e \n", b_C_r(0,5,Nsrc,0,0), b_C_i(0,5,Nsrc,0,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,0,Nsrc,0,0), b_C_i(Nsnk,0,Nsrc,0,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,1,Nsrc,0,0), b_C_i(Nsnk,1,Nsrc,0,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,2,Nsrc,0,0), b_C_i(Nsnk,2,Nsrc,0,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,3,Nsrc,0,0), b_C_i(Nsnk,3,Nsrc,0,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,4,Nsrc,0,0), b_C_i(Nsnk,4,Nsrc,0,0) );
   printf("H-H non-zero? %4.9e + I %4.9e \n", b_C_r(Nsnk,5,Nsrc,0,0), b_C_i(Nsnk,5,Nsrc,0,0) );
   }

    // symmetrize and such
      for (int m=0; m<Nsrc; m++)
         for (int n=0; n<Nsnk; n++)
            for (int t=0; t<Lt; t++) {
                  double number0r;
                  double number0i;
                  double number1r;
                  double number1i;
                  double number2r;
                  double number2i;
                  double number3r;
                  double number3i;
                  double number4r;
                  double number4i;
                  double number5r;
                  double number5i;
                  double this_number0r = b_C_r(n,0,m,rank,t);
                  double this_number0i = b_C_i(n,0,m,rank,t);
                  double this_number1r = b_C_r(n,1,m,rank,t);
                  double this_number1i = b_C_i(n,1,m,rank,t);
                  double this_number2r = b_C_r(n,2,m,rank,t);
                  double this_number2i = b_C_i(n,2,m,rank,t);
                  double this_number3r = b_C_r(n,3,m,rank,t);
                  double this_number3i = b_C_i(n,3,m,rank,t);
                  double this_number4r = b_C_r(n,4,m,rank,t);
                  double this_number4i = b_C_i(n,4,m,rank,t);
                  double this_number5r = b_C_r(n,5,m,rank,t);
                  double this_number5i = b_C_i(n,5,m,rank,t);
               MPI_Allreduce(&this_number0r, &number0r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number0i, &number0i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1r, &number1r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1i, &number1i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2r, &number2r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2i, &number2i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number3r, &number3r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number3i, &number3i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4r, &number4r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4i, &number4i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number5r, &number5r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number5i, &number5i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Barrier(MPI_COMM_WORLD);
                 C_re[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2r - number3r - number4r + number5r);
                 C_re[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number0r;
                 C_re[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2r + number3r + number4r + number5r);
                 C_re[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number1r;
                 C_im[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2i - number3i - number4i + number5i);
                 C_im[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number0i;
                 C_im[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2i + number3i + number4i + number5i);
                 C_im[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number1i;
            }
       for (int m=Nsrc; m<Nsrc+NsrcHex; m++)
         for (int n=0; n<Nsnk; n++)
            for (int t=0; t<Lt; t++) {
                  double number0r;
                  double number0i;
                  double number1r;
                  double number1i;
                  double number2r;
                  double number2i;
                  double number3r;
                  double number3i;
                  double number4r;
                  double number4i;
                  double number5r;
                  double number5i;
                  double this_number0r = b_C_r(n,0,m,rank,t);
                  double this_number0i = b_C_i(n,0,m,rank,t);
                  double this_number1r = b_C_r(n,1,m,rank,t);
                  double this_number1i = b_C_i(n,1,m,rank,t);
                  double this_number2r = b_C_r(n,2,m,rank,t);
                  double this_number2i = b_C_i(n,2,m,rank,t);
                  double this_number3r = b_C_r(n,3,m,rank,t);
                  double this_number3i = b_C_i(n,3,m,rank,t);
                  double this_number4r = b_C_r(n,4,m,rank,t);
                  double this_number4i = b_C_i(n,4,m,rank,t);
                  double this_number5r = b_C_r(n,5,m,rank,t);
                  double this_number5i = b_C_i(n,5,m,rank,t);
               MPI_Allreduce(&this_number0r, &number0r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number0i, &number0i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1r, &number1r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1i, &number1i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2r, &number2r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2i, &number2i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number3r, &number3r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number3i, &number3i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4r, &number4r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4i, &number4i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number5r, &number5r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number5i, &number5i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Barrier(MPI_COMM_WORLD);
                 C_re[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number4r - number5r);
                 C_re[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number0r;
                 C_re[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2r + number3r);
                 C_re[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number1r;
                 C_im[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number4i - number5i);
                 C_im[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number0i;
                 C_im[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2i + number3i);
                 C_im[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number1i;
            }
      for (int m=0; m<Nsrc; m++)
         for (int n=Nsnk; n<Nsnk+NsnkHex; n++)
            for (int t=0; t<Lt; t++) {
                  double number0r;
                  double number0i;
                  double number1r;
                  double number1i;
                  double number2r;
                  double number2i;
                  double number3r;
                  double number3i;
                  double number4r;
                  double number4i;
                  double number5r;
                  double number5i;
                  double this_number0r = b_C_r(n,0,m,rank,t);
                  double this_number0i = b_C_i(n,0,m,rank,t);
                  double this_number1r = b_C_r(n,1,m,rank,t);
                  double this_number1i = b_C_i(n,1,m,rank,t);
                  double this_number2r = b_C_r(n,2,m,rank,t);
                  double this_number2i = b_C_i(n,2,m,rank,t);
                  double this_number3r = b_C_r(n,3,m,rank,t);
                  double this_number3i = b_C_i(n,3,m,rank,t);
                  double this_number4r = b_C_r(n,4,m,rank,t);
                  double this_number4i = b_C_i(n,4,m,rank,t);
                  double this_number5r = b_C_r(n,5,m,rank,t);
                  double this_number5i = b_C_i(n,5,m,rank,t);
               MPI_Allreduce(&this_number0r, &number0r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number0i, &number0i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1r, &number1r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1i, &number1i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2r, &number2r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2i, &number2i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number3r, &number3r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number3i, &number3i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4r, &number4r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4i, &number4i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number5r, &number5r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number5i, &number5i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Barrier(MPI_COMM_WORLD);
                 C_re[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number4r - number5r);
                 C_re[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number0r;
                 C_re[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2r + number3r);
                 C_re[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number1r;
                 C_im[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number4i - number5i);
                 C_im[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number0i;
                 C_im[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = 1/sqrt(2) * (number2i + number3i);
                 C_im[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = -number1i;
            }
      for (int m=Nsrc; m<Nsrc+NsrcHex; m++)
         for (int n=Nsnk; n<Nsnk+NsnkHex; n++)
            for (int t=0; t<Lt; t++) {
                  double number0r;
                  double number0i;
                  double number1r;
                  double number1i;
                  double number2r;
                  double number2i;
                  double number4r;
                  double number4i;
                  double this_number0r = b_C_r(n,0,m,rank,t);
                  double this_number0i = b_C_i(n,0,m,rank,t);
                  double this_number1r = b_C_r(n,1,m,rank,t);
                  double this_number1i = b_C_i(n,1,m,rank,t);
                  double this_number2r = b_C_r(n,2,m,rank,t);
                  double this_number2i = b_C_i(n,2,m,rank,t);
                  double this_number4r = b_C_r(n,4,m,rank,t);
                  double this_number4i = b_C_i(n,4,m,rank,t);
               MPI_Allreduce(&this_number0r, &number0r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number0i, &number0i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1r, &number1r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number1i, &number1i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2r, &number2r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number2i, &number2i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4r, &number4r, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Allreduce(&this_number4i, &number4i, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
               MPI_Barrier(MPI_COMM_WORLD);
                 C_re[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number4r;
                 C_re[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number0r;
                 C_re[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number2r;
                 C_re[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number1r;
                 C_im[index_4d(0,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number4i;
                 C_im[index_4d(1,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number0i;
                 C_im[index_4d(2,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number2i;
                 C_im[index_4d(3,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)] = number1i;
            }

   if (rank == 0) {
   for (int b=0; b<4; b++) {
      printf("\n");
      for (int m=0; m<Nsrc+NsrcHex; m++)
         for (int n=0; n<Nsnk+NsnkHex; n++)
            for (int t=0; t<Lt; t++) {
                  printf("r=%d, m=%d, n=%d, t=%d: %4.9e + I (%4.9e) \n", b, m, n, t, C_re[index_4d(b,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)],  C_im[index_4d(b,m,n,t, Nsrc+NsrcHex,Nsnk+NsnkHex,Lt)]);
            }
   }
   }

   free(snk_color_weights_1);
   free(snk_color_weights_2);
   free(snk_color_weights_r1);
   free(snk_color_weights_r2_1);
   free(snk_color_weights_r2_2);
   free(snk_color_weights_r3);
   free(snk_spin_weights_1);
   free(snk_spin_weights_2);
   free(snk_spin_weights_r1);
   free(snk_spin_weights_r2_1);
   free(snk_spin_weights_r2_2);
   free(snk_spin_weights_r3);
}

#ifdef __cplusplus
}  // extern "C"
#endif

{'repo_name': 'Tiramisu-Compiler/tiramisu', 'stars': '620', 'repo_language': 'Jupyter Notebook', 'file_name': 'wrapper_test_86.cpp', 'mime_type': 'text/x-c', 'hash': -3142832547992759443, 'source_dataset': 'data'}


"""
Convert use of sys.exitfunc to use the atexit module.
"""

# Author: Benjamin Peterson

from lib2to3 import pytree, fixer_base
from lib2to3.fixer_util import Name, Attr, Call, Comma, Newline, syms

class FixExitfunc(fixer_base.BaseFix):
    keep_line_order = True
    BM_compatible = True

    PATTERN = """
              (
                  sys_import=import_name<'import'
                      ('sys'
                      |
                      dotted_as_names< (any ',')* 'sys' (',' any)* >
                      )
                  >
              |
                  expr_stmt<
                      power< 'sys' trailer< '.' 'exitfunc' > >
                  '=' func=any >
              )
              """

    def __init__(self, *args):
        super(FixExitfunc, self).__init__(*args)

    def start_tree(self, tree, filename):
        super(FixExitfunc, self).start_tree(tree, filename)
        self.sys_import = None

    def transform(self, node, results):
        # First, find the sys import. We'll just hope it's global scope.
        if "sys_import" in results:
            if self.sys_import is None:
                self.sys_import = results["sys_import"]
            return

        func = results["func"].clone()
        func.prefix = ""
        register = pytree.Node(syms.power,
                               Attr(Name("atexit"), Name("register"))
                               )
        call = Call(register, [func], node.prefix)
        node.replace(call)

        if self.sys_import is None:
            # That's interesting.
            self.warning(node, "Can't find sys import; Please add an atexit "
                             "import at the top of your file.")
            return

        # Now add an atexit import after the sys import.
        names = self.sys_import.children[1]
        if names.type == syms.dotted_as_names:
            names.append_child(Comma())
            names.append_child(Name("atexit", " "))
        else:
            containing_stmt = self.sys_import.parent
            position = containing_stmt.children.index(self.sys_import)
            stmt_container = containing_stmt.parent
            new_import = pytree.Node(syms.import_name,
                              [Name("import"), Name("atexit", " ")]
                              )
            new = pytree.Node(syms.simple_stmt, [new_import])
            containing_stmt.insert_child(position + 1, Newline())
            containing_stmt.insert_child(position + 2, new)

{'repo_name': 'lzdnico/ClashWeb', 'stars': '356', 'repo_language': 'Python', 'file_name': 'stop.bat', 'mime_type': 'text/plain', 'hash': 8640659161107727162, 'source_dataset': 'data'}


Description

Gene predictions from Ensembl based on ESTs.

Methods

ESTs were mapped onto the genome using a combination of Exonerate, Blast
and Est_Genome, with a threshold defined as an overall percentage identity
of 90% and at least one exon having a percentage identity of 97% or higher.
The results were processed by merging the redundant ESTs and setting
splice sites to the most common ends, resulting in alternative spliced
forms. This evidence was processed by Genomewise, which finds the longest
ORF and assigns 5' and 3' UTRs.

Track Configuration

This track has an optional codon coloring feature that allows users to
quickly validate and compare gene predictions. To display codon colors,
select the genomic codons option from the Color track by
codons pull-down menu at the top of the track description page.
This page is accessed via the small button to the left of the track's
graphical display or through the link on the track's control menu. Click
here for more information about this feature.

After you have made your configuration selections, click the
Submit button to return to the tracks display page.

Credits

Thanks to Ensembl
for providing this annotation.

{'repo_name': 'ucscGenomeBrowser/kent', 'stars': '114', 'repo_language': 'C', 'file_name': 'wgEncodeRbpSuper.html', 'mime_type': 'text/html', 'hash': -472197988913908209, 'source_dataset': 'data'}


//
//  YJSystem.m
//  YJCocoa
//
//  HomePage:https://github.com/937447974/YJCocoa
//  YJ技术支持群:557445088
//
//  Created by 阳君 on 16/5/11.
//  Copyright © 2016年 YJ. All rights reserved.
//

#import "YJSystem.h"

// 主线程运行
void dispatch_async_main(dispatch_block_t block) {
    dispatch_async(dispatch_get_main_queue(), block);
}

// 后台运行
void dispatch_async_background(dispatch_block_t block) {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_async(queue, block);
}

// 异步UI执行
void dispatch_async_UI(dispatch_block_t block) {
    dispatch_async_background(^{
        dispatch_async_main(block);
    });
}

// 主线程延时执行
void dispatch_after_main(int64_t delayInSeconds, dispatch_block_t block) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}

@implementation YJSystem

@end

{'repo_name': '937447974/YJTableViewFactory', 'stars': '128', 'repo_language': 'Objective-C', 'file_name': 'contents.xcworkspacedata', 'mime_type': 'text/xml', 'hash': 1304945988232525144, 'source_dataset': 'data'}


/* Lisp functions pertaining to editing.
   Copyright (C) 1985 Richard M. Stallman.

This file is part of GNU Emacs.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the GNU Emacs General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
GNU Emacs, but only under the conditions described in the
GNU Emacs General Public License.   A copy of this license is
supposed to have been given to you along with GNU Emacs so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  */

#include "config.h"
#include <pwd.h>
#include "lisp.h"
#include "buffer.h"
#include "window.h"

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

Lisp_Object ml_apply ();

/* Some static data, and a function to initialize it for each run */

static char user_real_name[12];	/* login ID of current user */
static char user_full_name[50];	/* full name of current user */

static char system_name[40];
static char *user_name;

void
init_editfns ()
{
  register char *p, *q, *r;
  register int c;
  int first;
  struct passwd *pw;	/* password entry for the current user */
  extern char *index ();

  /* Don't bother with this on initial start when just dumping out */
  if (!NULL (Vpurify_flag))
    return;

  pw = (struct passwd *) getpwuid (getuid ());
  strncpy (user_real_name, pw->pw_name, sizeof user_real_name);

  user_name = (char *) getenv ("USER");
#ifdef USG
  if (!user_name)
    user_name = (char *) getenv ("LOGNAME");		/* USG equivalent */
#endif
  if (!user_name)
    user_name = user_real_name;

  if (strcmp (user_name, user_real_name))
    pw = (struct passwd *) getpwnam (user_name);

#ifndef AMPERSAND_FULL_NAME
  if (pw == 0)
    strcpy (user_full_name, "unknown");
  else
    strncpy (user_full_name, USER_FULL_NAME, sizeof user_full_name);
  p = index (user_full_name, ',');
  if (p) *p = 0;
#else
  if (pw == 0)
    p = "unknown";
  else
    p = USER_FULL_NAME;
  q = user_full_name; r = user_name; first = 1;

  for (; (*p != 0) && (*p != ','); p++)
    {
      if (*p == '&')
	{
	  if (*r != 0)
	    {
	      *q = *r++;
	      if ((*q >= 'a') && (*q <= 'z'))
		*q -= 32;
	      for (q++; *r != 0; r++)
		{
		  if (q == &user_full_name[sizeof user_full_name - 1])
		    break;
		  *q++ = *r;
		}
	    }
	}
      else
	*q++ = *p;
      if (q == &user_full_name[sizeof user_full_name - 2])
	break;
    }
  *q = 0;
#endif /* AMPERSAND_FULL_NAME */

  p = (char *) get_system_name ();
  if (p == 0 || *p == 0)
    p = "Bogus System Name";
  strncpy (system_name, p, sizeof system_name);
  p = system_name;
  while (*p)
    {
      if (*p < ' ')
	*p = 0;
      else
	if (*p == ' ')
	  *p = '-';
      p++;
    }
}

DEFUN ("char-to-string", Fchar_to_string, Schar_to_string, 1, 1, 0,
  "Convert arg CHAR to a string containing that character.")
  (n)
     Lisp_Object n;
{
  char c;
  CHECK_NUMBER (n, 0);

  c = XINT (n);
  return make_string (&c, 1);
}

DEFUN ("string-to-char", Fstring_to_char, Sstring_to_char, 1, 1, 0,
  "Convert arg STRING to a character, the first character of that string.")
  (str)
     Lisp_Object str;
{
  Lisp_Object val;
  CHECK_STRING (str, 0);

  if (XSTRING (str)->size)
    XFASTINT (val) = ((unsigned char *) XSTRING (str)->data)[0];
  else
    XFASTINT (val) = 0;
  return val;
}

static Lisp_Object
buildmark (val)
{
  Lisp_Object mark;
  mark = Fmake_marker ();
  Fset_marker (mark, make_number (val), Qnil);
  return mark;
}

DEFSIMPLE ("point", Fpoint, Spoint,
  "Return value of point, as an integer.\n\
Beginning of buffer is position (point-min)",
	   Lisp_Int, XSETINT, point)

DEFUN ("point-marker", Fpoint_marker, Spoint_marker, 0, 0, 0,
   "Return value of point, as a marker object.")
  ()
{
  return buildmark (point);
}

DEFUN ("goto-char", Fgoto_char, Sgoto_char, 1, 1, "nGoto char: ",
  "One arg, a number.  Set point to that number.\n\
Beginning of buffer is position (point-min), end is (point-max).")
  (n)
     Lisp_Object n;
{
  int charno;
  CHECK_NUMBER_COERCE_MARKER (n, 0);
  charno = XINT (n);
  if (charno < FirstCharacter) charno = FirstCharacter;
  if (charno > NumCharacters) charno = NumCharacters + 1;
  SetPoint (charno);
  return n;
}

DEFUN ("region-beginning", Fregion_beginning, Sregion_beginning, 0, 0, 0,
  "Return position of beginning of region, as an integer.")
  ()
{
  register int tem;
  if (NULL (bf_cur->mark))
    error ("There is no region now");
  tem = marker_position (bf_cur->mark);
  return make_number (min (point, tem));
}

DEFUN ("region-end", Fregion_end, Sregion_end, 0, 0, 0,
  "Return position of end of region, as an integer.")
  ()
{
  register int tem;
  if (NULL (bf_cur->mark))
    error ("There is no region now");
  tem = marker_position (bf_cur->mark);
  return make_number (max (point, tem));
}

DEFUN ("mark", Fmark, Smark, 0, 0, 0,
  "Return this buffer's mark value as integer, or nil if no mark.")
  ()
{
  if (!NULL (bf_cur->mark))
    return Fmarker_position (bf_cur->mark);
  return Qnil;
}

DEFUN ("mark-marker", Fmark_marker, Smark_marker, 0, 0, 0,
  "Return this buffer's mark, as a marker object, or nil if no mark.\n\
Watch out!  Moving this marker changes the buffer's mark.")
  ()
{
  return bf_cur->mark;
}

DEFUN ("set-mark", Fset_mark, Sset_mark, 1, 1, "",
  "Set this buffer's mark to POS;\n\
Argument is character position, or nil to clear out the mark.")
  (pos)
     Lisp_Object pos;
{
  if (NULL (pos))
    {
      bf_cur->mark = Qnil;
      return Qnil;
    }
  CHECK_NUMBER_COERCE_MARKER (pos, 0);

  if (NULL (bf_cur->mark))
    bf_cur->mark = Fmake_marker ();

  Fset_marker (bf_cur->mark, pos, Qnil);
  return pos;
}

Lisp_Object
save_excursion_save ()
{
  Lisp_Object oldpoint, oldmark;
  int visible = XBUFFER (XWINDOW (selected_window)->buffer) == bf_cur;

  oldpoint = Fpoint_marker ();

  if (!NULL (bf_cur->mark))
    oldmark = Fcopy_marker (bf_cur->mark);
  else
    oldmark = Qnil;

  return Fcons (oldpoint, Fcons (oldmark, visible ? Qt : Qnil));
}

Lisp_Object
save_excursion_restore (info)
     Lisp_Object info;
{
  Lisp_Object tem;

  tem = Fmarker_buffer (Fcar (info));
  /* If buffer being returned to is now deleted, avoid error */
  /* Otherwise could get error here while unwinding to top level
     and crash */
  /* In that case, Fmarker_buffer returns nil now.  */
  if (NULL (tem))
    return Qnil;
  Fset_buffer (tem);
  Fgoto_char (Fcar (info));
  unchain_marker (Fcar (info));
  tem = Fcar (Fcdr (info));
  Fset_mark (tem);
  if (!NULL (tem))
    unchain_marker (tem);
  tem = Fcdr (Fcdr (info));
  if (!NULL (tem) && bf_cur != XBUFFER (XWINDOW (selected_window)->buffer))
    Fswitch_to_buffer (Fcurrent_buffer (), Qnil);
  return Qnil;
}

DEFUN ("save-excursion", Fsave_excursion, Ssave_excursion, 0, UNEVALLED, 0,
  "Save point (and mark), execute BODY, then restore point and mark.\n\
Executes BODY just like PROGN.  Point and mark values are restored\n\
even in case of abnormal exit (throw or error).")
  (args)
     Lisp_Object args;
{
  Lisp_Object val;
  int count = specpdl_ptr - specpdl;

  record_unwind_protect (save_excursion_restore, save_excursion_save ());

  val = Fprogn (args);
  unbind_to (count);
  return val;
}

DEFSIMPLE ("buffer-size", Fbufsize, Sbufsize,
	   "Return the number of characters in the current buffer.",
	   Lisp_Int, XSETINT, bf_s1 + bf_s2)

DEFSIMPLE ("point-min", Fpoint_min, Spoint_min,
	   "Return the minimum permissible value of point in the current buffer.\n\
This is 1, unless a clipping restriction is in effect.",
	   Lisp_Int, XSETINT, FirstCharacter)

DEFUN ("point-min-marker", Fpoint_min_marker, Spoint_min_marker, 0, 0, 0,
  "Return a marker to the beginning of the currently visible part of the buffer.\n\
This is the beginning, unless a clipping restriction is in effect.")
  ()
{
  return buildmark (FirstCharacter);
}

DEFSIMPLE ("point-max", Fpoint_max, Spoint_max,
  "Return the maximum permissible value of point in the current buffer.\n\
This is (1+ (buffer-size)), unless a clipping restriction is in effect,\n\
in which case it is less.",
	   Lisp_Int, XSETINT, NumCharacters+1)

DEFUN ("point-max-marker", Fpoint_max_marker, Spoint_max_marker, 0, 0, 0,
  "Return a marker to the end of the currently visible part of the buffer.\n\
This is the actual end, unless a clipping restriction is in effect.")
  ()
{
  return buildmark (NumCharacters+1);
}

DEFSIMPLE ("following-char", Ffollchar, Sfollchar,
	   "Return the character following point, as a number.",
	   Lisp_Int, XSETINT, point>NumCharacters ? 0 : CharAt(point))
DEFSIMPLE ("preceding-char", Fprevchar, Sprevchar,
	   "Return the character preceding point, as a number.",
	   Lisp_Int, XSETINT, point<=FirstCharacter ? 0 : CharAt(point-1))

DEFPRED ("bobp", Fbobp, Sbobp,
  "Return T if point is at the beginning of the buffer.\n\
If the buffer is narrowed, this means the beginning of the narrowed part.",
	 point<=FirstCharacter)
DEFPRED ("eobp", Feobp, Seobp,
  "Return T if point is at the end of the buffer.\n\
If the buffer is narrowed, this means the end of the narrowed part.",
	 point>NumCharacters)
DEFPRED ("bolp", Fbolp, Sbolp,
  "Return T if point is at the beginning of a line.",
	 point<=FirstCharacter || CharAt(point-1)=='\n')
DEFPRED ("eolp", Feolp, Seolp,
  "Return T if point is at the end of a line.\n\
`End of a line' includes point being at the end of the buffer.",
	 point>NumCharacters || CharAt(point)=='\n')

DEFUN ("char-after", Fchar_after, Schar_after, 1, 1, 0,
  "One arg, POS, a number.  Return the character in the current buffer\n\
at position POS.\n\
If POS is out of range, the value is NIL.")
  (pos)
     Lisp_Object pos;
{
  Lisp_Object val;
  CHECK_NUMBER_COERCE_MARKER (pos, 0);
  if (XINT (pos) < FirstCharacter || XINT (pos) > NumCharacters) return Qnil;

  XFASTINT (val) = CharAt(XINT (pos));
  return val;
}

DEFUN ("user-login-name", Fuser_login_name, Suser_login_name, 0, 0, "",
  "Return the name under which user logged in, as a string.")
  ()
{
  return build_string (user_name);
}

DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
  0, 0, "",
  "Return the name of the user's real uid, as a string.\n\
Differs from user-login-name when running under su.")
  ()
{
  return build_string (user_real_name);
}

DEFUN ("user-full-name", Fuser_full_name, Suser_full_name, 0, 0, "",
  "Return the full name of the user logged in, as a string.")
  ()
{
  return build_string (user_full_name);
}

DEFUN ("system-name", Fsystem_name, Ssystem_name, 0, 0, "",
  "Return the name of the machine you are running on, as a string.")
  ()
{
  return build_string (system_name);
}

DEFUN ("current-time-string", Fcurrent_time_string, Scurrent_time_string, 0, 0, 0,
  "Return the current time, as a human-readable string.")
  ()
{
  long now = time ( (long *) 0);
  char *tem = (char *) ctime (&now);
  tem [24] = 0;
  return build_string (tem);
}

DEFUN ("insert", Finsert, Sinsert, 0, MANY, 0,
  "Any number of args, strings or chars.  Insert them after point, moving point forward.")
  (nargs, args)
     int nargs;
     Lisp_Object *args;
{
  int argnum;
  Lisp_Object tem;
  char str[1];

  for (argnum = 0; argnum < nargs; argnum++)
    {
      tem = args[argnum];
    retry:
      if (XTYPE (tem) == Lisp_Int)
	{
	  str[0] = XINT (tem);
	  InsCStr (str, 1);
	}
      else if (XTYPE (tem) == Lisp_String)
	{
	  InsCStr (XSTRING (tem)->data, XSTRING (tem)->size);
	}
      else
	{
	  tem = wrong_type_argument (Qchar_or_string_p, tem);
	  goto retry;
	}
    }
  return Qnil;
}

DEFUN ("insert-before-markers", Finsert_before_markers, Sinsert_before_markers, 0, MANY, 0,
  "Any number of args, strings or chars.  Insert them after point,\n\
moving point forward.  Also, any markers pointing at the insertion point\n\
get relocated to point after the newly inserted text.")
  (nargs, args)
     int nargs;
     Lisp_Object *args;
{
  int argnum;
  Lisp_Object tem;
  char str[1];

  for (argnum = 0; argnum < nargs; argnum++)
    {
      tem = args[argnum];
    retry:
      if (XTYPE (tem) == Lisp_Int)
	{
	  str[0] = XINT (tem);
	  insert_before_markers (str, 1);
	}
      else if (XTYPE (tem) == Lisp_String)
	{
	  insert_before_markers (XSTRING (tem)->data, XSTRING (tem)->size);
	}
      else
	{
	  tem = wrong_type_argument (Qchar_or_string_p, tem);
	  goto retry;
	}
    }
  return Qnil;
}

/* Return a string with the contents of the current region */

DEFUN ("buffer-substring", Fbuffer_substring, Sbuffer_substring, 2, 2, 0,
  "Return the contents of part of the current buffer as a string.\n\
The two arguments specify the start and end, as character numbers.")
  (b, e)
     Lisp_Object b, e;
{
  int beg, end;

  validate_region (&b, &e);
  beg = XINT (b);
  end = XINT (e);

  if (beg <= bf_s1 && end > bf_s1)
      GapTo (beg);
  return make_string (&CharAt (beg), end - beg);
}

DEFUN ("buffer-string", Fbuffer_string, Sbuffer_string, 0, 0, 0,
  "Return the contents of the current buffer as a string.")
  ()
{
  if (FirstCharacter <= bf_s1 && NumCharacters + 1 > bf_s1)
      GapTo (FirstCharacter);
  return make_string (&CharAt (FirstCharacter), NumCharacters + 1 - FirstCharacter);
}

DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring,
  1, 3, 0,
  "Insert before point a substring of the contents buffer BUFFER.\n\
BUFFER may be a buffer or a buffer name.\n\
Arguments START and END are character numbers specifying the substring.\n\
They default to the beginning and the end of BUFFER.")
  (buf, b, e)
     Lisp_Object buf, b, e;
{
    int beg, end, exch;

  buf = Fget_buffer (buf);
  if (XBUFFER (buf) == bf_cur)
    error ("Cannot insert buffer into itself");

  if (NULL (b))
    beg = XBUFFER (buf)->text.head_clip - 1;
  else
    {
      CHECK_NUMBER_COERCE_MARKER (b, 0);
      beg = XINT (b) - 1;
    }
  if (NULL (e))
    end = XBUFFER (buf)->text.size1 + XBUFFER (buf)->text.size2
			- XBUFFER (buf)->text.tail_clip;
  else
    {
      CHECK_NUMBER_COERCE_MARKER (e, 1);
      end = XINT (e) - 1;
    }

  if (beg > end)
    exch = beg, beg = end, end = exch;

  if (!(XBUFFER (buf)->text.head_clip - 1 <= beg
	&& beg <= end
        && end <= XBUFFER (buf)->text.size1 + XBUFFER (buf)->text.size2
			- XBUFFER (buf)->text.tail_clip))
    args_out_of_range (b, e);

  if (beg < XBUFFER (buf)->text.size1)
    {
      InsCStr (XBUFFER (buf)->text.p1 + 1 + beg, min (end, XBUFFER (buf)->text.size1) - beg);
      beg = min (end, XBUFFER (buf)->text.size1);
    }
  if (beg < end)
    InsCStr (XBUFFER (buf)->text.p2 + 1 + beg, end - beg);

  return Qnil;
}

DEFUN ("subst-char-in-region", Fsubst_char_in_region,
  Ssubst_char_in_region, 4, 5, 0,
  "From START to END, replace FROMCHAR with TOCHAR each time it occurs.\n\
If optional arg NOUNDO is non-nil, don't record this change for undo\n\
and don't mark the buffer as really changed.")
  (start, end, fromchar, tochar, noundo)
     Lisp_Object start, end, fromchar, tochar, noundo;
{
  register int pos, stop, look;

  validate_region (&start, &end);
  CHECK_NUMBER (fromchar, 2);
  CHECK_NUMBER (tochar, 3);

  pos = XINT (start);
  stop = XINT (end);
  if (!NULL (bf_cur->read_only))
    Fbarf_if_buffer_read_only();

  look = XINT (fromchar);

  while (pos < stop)
    {
      if (CharAt (pos) == look)
	{
	  if (NULL (noundo))
	    RecordChange (pos, 1);
	  CharAt (pos) = XINT (tochar);
	}
      pos++;
    }
  modify_region (pos, stop);

  return Qnil;
}

DEFUN ("delete-region", Fdelete_region, Sdelete_region, 2, 2, "r",
  "Delete the text between point and mark.\n\
When called from a program, expects two arguments,\n\
character numbers specifying the stretch to be deleted.")
  (b, e)
     Lisp_Object b, e;
{
  validate_region (&b, &e);
  del_range (XINT (b), XINT (e));
  return Qnil;
}

DEFUN ("widen", Fwiden, Swiden, 0, 0, "",
  "Remove restrictions from current buffer, allowing full text to be seen and edited.")
  ()
{
  bf_cur->text.head_clip = bf_head_clip = 1;
  bf_cur->text.tail_clip = bf_tail_clip = 0;
  clip_changed = 1;
  return Qnil;
}

DEFUN ("narrow-to-region", Fnarrow_to_region, Snarrow_to_region, 2, 2, "r",
  "Restrict editing in current buffer to text between present values of point and mark.\n\
Use  widen  to undo the effects of this command.\n\
Called non-interactively, takes two arguments; character numbers which\n\
specify the stretch to which to restrict.")
  (b, e)
     Lisp_Object b, e;
{
  int i;

  CHECK_NUMBER_COERCE_MARKER (b, 0);
  CHECK_NUMBER_COERCE_MARKER (e, 1);

  if (XINT (b) > XINT (e))
    {
      i = XFASTINT (b);
      b = e;
      XFASTINT (e) = i;
    }

  if (!(1 <= XINT (b) && XINT (b) <= XINT (e)
        && XINT (e) <= bf_s1 + bf_s2 + 1))
    args_out_of_range (b, e);

  bf_cur->text.head_clip = bf_head_clip = XFASTINT (b);
  bf_cur->text.tail_clip = bf_tail_clip = bf_s1 + bf_s2 + 1 - XFASTINT (e);
  if (point < XFASTINT (b))
    SetPoint (XFASTINT (b));
  if (point > XFASTINT (e))
    SetPoint (XFASTINT (e));
  clip_changed = 1;
  return Qnil;
}

Lisp_Object
save_restriction_save ()
{
  Lisp_Object ml, mh;
  /* Note: I tried using markers here, but it does not win
     because insertion at the end of the saved region
     does not advance mh and is considered "outside" the saved region. */
  XFASTINT (ml) = bf_head_clip;
  XFASTINT (mh) = bf_tail_clip;

  return Fcons (Fcurrent_buffer (), Fcons (ml, mh));
}

Lisp_Object
save_restriction_restore (data)
     Lisp_Object data;
{
  register struct buffer *old = bf_cur;
  register int newhead, newtail;

  Fset_buffer (XCONS (data)->car);

  data = XCONS (data)->cdr;

  newhead = XINT (XCONS (data)->car);
  newtail = XINT (XCONS (data)->cdr);
  if (newhead + newtail > bf_s1 + bf_s2 + 1)
    {
      newhead = 1;
      newtail = 0;
    }
  bf_cur->text.head_clip = bf_head_clip = newhead;
  bf_cur->text.tail_clip = bf_tail_clip = newtail;
  clip_changed = 1;

  /* If point is outside the new visible range, move it inside. */
  if (point < FirstCharacter)
    SetPoint (FirstCharacter);
  if (point > NumCharacters+1)
    SetPoint (NumCharacters+1);

  SetBfp (old);
  return Qnil;
}

DEFUN ("save-restriction", Fsave_restriction, Ssave_restriction, 0, UNEVALLED, 0,
  "Execute the body, undoing at the end any changes to current buffer's restrictions.\n\
Changes to restrictions are made by narrow-to-region or by widen.\n\
Thus, the restrictions are the same after this function as they were before it.\n\
The value returned is that returned by the last form in the body.\n\
\n\
This function can be confused if, within the body, you widen\n\
and then make changes outside the area within the saved restrictions.\n\
\n\
Note: if you are using both save-excursion and save-restriction,\n\
use save-excursion outermost.")
  (body)
     Lisp_Object body;
{
  Lisp_Object val;
  int count = specpdl_ptr - specpdl;

  record_unwind_protect (save_restriction_restore, save_restriction_save ());
  val = Fprogn (body);
  unbind_to (count);
  return val;
}

DEFUN ("message", Fmessage, Smessage, 1, MANY, 0,
  "Print a one-line message at the bottom of the screen.\n\
The first argument is a control string.\n\
It may contain %s or %d or %c to print successive following arguments.\n\
%s means print an argument as a string, %d means print as number in decimal,\n\
%c means print a number as a single character.\n\
The argument used by %s must be a string or a symbol;\n\
the argument used by %d or %c must be a number.")
  (nargs, args)
     int nargs;
     Lisp_Object *args;
{
  Lisp_Object val;

  val = Fformat (nargs, args);
  message ("%s", XSTRING (val)->data);
  return val;
}

DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
  "Format a string out of a control-string and arguments.\n\
The first argument is a control string.\n\
It, and subsequent arguments substituted into it, become the value, which is a string.\n\
It may contain %s or %d or %c to substitute successive following arguments.\n\
%s means print an argument as a string, %d means print as number in decimal,\n\
%c means print a number as a single character.\n\
The argument used by %s must be a string or a symbol;\n\
the argument used by %d, %b, %o, %x or %c must be a number.")
  (nargs, args)
     int nargs;
     register Lisp_Object *args;
{
  register int i;
  register int total = 5;
  char *buf;
  register unsigned char **strings = (unsigned char **) alloca (nargs * sizeof (char *));

  for (i = 0; i < nargs; i++)
    {
      if (XTYPE (args[i]) == Lisp_Symbol)
	{
	  strings[i] = XSYMBOL (args[i])->name->data;
	  total += XSYMBOL (args[i])->name->size;
	}
      else if (XTYPE (args[i]) == Lisp_String)
	{
	  strings[i] = XSTRING (args[i])->data;
	  total += XSTRING (args[i])->size;
	}
      else if (XTYPE (args[i]) == Lisp_Int)
	{
	  strings[i] = (unsigned char *) XINT (args[i]);
	  total += 10;
	}
      else
	{
	  strings[i] = (unsigned char *) "??";
	  total += 2;
	}
    }

  /* Format it in bigger and bigger buf's until it all fits. */

  while (1)
    {
      buf = (char *) alloca (total + 1);
      buf[total - 1] = 0;

      doprnt (buf, total + 1, strings[0], strings + 1);
      if (buf[total - 1] == 0)
	break;

      total *= 2;
    }

  return build_string (buf);
}

/* VARARGS 1 */
Lisp_Object
#ifdef NO_ARG_ARRAY
format1 (string1, arg0, arg1, arg2, arg3, arg4)
     Lisp_Object arg0, arg1, arg2, arg3, arg4;
#else
format1 (string1)
#endif
     char *string1;
{
  char buf[100];
#ifdef NO_ARG_ARRAY
  Lisp_Object args[5];
  args[0] = arg0;
  args[1] = arg1;
  args[2] = arg2;
  args[3] = arg3;
  args[4] = arg4;
  doprnt (buf, sizeof buf, string1, args);
#else
  doprnt (buf, sizeof buf, string1, &string1 + 1);
#endif
  return build_string (buf);
}

DEFUN ("char-equal", Fchar_equal, Schar_equal, 2, 2, 0,
  "T if args (both characters (numbers)) match.  May ignore case.\n\
Case is ignored if the current buffer specifies to do so.")
  (c1, c2)
     Lisp_Object c1, c2;
{
  extern char downcase_table[];  /* From search.c */

  CHECK_NUMBER (c1, 0);
  CHECK_NUMBER (c2, 1);

  if (!NULL (bf_cur->case_fold_search)
      ? downcase_table[XINT (c1)] == downcase_table[XINT (c2)]
      : XINT (c1) == XINT (c2))
    return Qt;
  return Qnil;
}

DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, "sEnvironment variable: ",
  "One arg VAR, a string. Return the value of environment variable VAR, as a string.")
  (str)
     Lisp_Object str;
{
  char *val;
  CHECK_STRING (str, 0);
  val = (char *) getenv (XSTRING (str)->data);
  if (!val)
    return Qnil;
  return build_string (val);
}

void
syms_of_editfns ()
{
  defsubr (&Schar_equal);
  defsubr (&Sgoto_char);
  defsubr (&Sstring_to_char);
  defsubr (&Schar_to_string);
  defsubr (&Sbuffer_substring);
  defsubr (&Sbuffer_string);

  defsubr (&Spoint_marker);
  defalias (&Spoint_marker, "dot-marker");
  defsubr (&Smark_marker);
  defsubr (&Spoint);
  defalias (&Spoint, "dot");
  defsubr (&Sregion_beginning);
  defsubr (&Sregion_end);
  defsubr (&Smark);
  defsubr (&Sset_mark);
  defsubr (&Ssave_excursion);

  defsubr (&Sbufsize);
  defsubr (&Spoint_max);
  defsubr (&Spoint_min);
  defalias (&Spoint_max, "dot-max");
  defalias (&Spoint_min, "dot-min");
  defsubr (&Spoint_min_marker);
  defsubr (&Spoint_max_marker);

  defsubr (&Sbobp);
  defsubr (&Seobp);
  defsubr (&Sbolp);
  defsubr (&Seolp);
  defsubr (&Sfollchar);
  defsubr (&Sprevchar);
  defsubr (&Schar_after);
  defsubr (&Sinsert);
  defsubr (&Sinsert_before_markers);

  defsubr (&Suser_login_name);
  defsubr (&Suser_real_login_name);
  defsubr (&Suser_full_name);
  defsubr (&Scurrent_time_string);
  defsubr (&Sgetenv);
  defsubr (&Ssystem_name);
  defsubr (&Smessage);
  defsubr (&Sformat);

  defsubr (&Sinsert_buffer_substring);
  defsubr (&Ssubst_char_in_region);
  defsubr (&Sdelete_region);
  defsubr (&Swiden);
  defsubr (&Snarrow_to_region);
  defsubr (&Ssave_restriction);
}

{'repo_name': 'larsbrinkhoff/emacs-history', 'stars': '172', 'repo_language': 'C', 'file_name': 'README-hyperbase', 'mime_type': 'text/plain', 'hash': 4062082292884347404, 'source_dataset': 'data'}


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>RakNet: NatPunchthrough</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />

RakNet
    4.0

Main Page

Related Pages

Modules

Namespaces

Classes

Files

Classes

NatPunchthrough

Plugins

Connect systems despite both systems being behind a router.
More...

Classes

class  	RakNet::NatPunchthroughClient

 	Client code for NATPunchthrough.  More...

class  	RakNet::NatPunchthroughServer

 	Server code for NATPunchthrough.  More...

class  	RakNet::UDPForwarder

 	Forwards UDP datagrams. Independent of RakNet's protocol.  More...

class  	RakNet::UDPProxyCoordinator

 	Middleman between UDPProxyServer and UDPProxyClient, maintaining a list of UDPProxyServer, and managing state for clients to find an available forwarding server.  More...

Detailed Description

Connect systems despite both systems being behind a router.

<address class="footer">
Generated on Mon Jun 2 2014 20:10:28 for RakNet by

 1.8.2
</address>

{'repo_name': 'facebookarchive/RakNet', 'stars': '2829', 'repo_language': 'HTML', 'file_name': 'FindOGRE.cmake', 'mime_type': 'text/x-c', 'hash': 3091837228649773234, 'source_dataset': 'data'}


Skins
=====

The base Video.js skin is made using HTML and CSS (although we use the [Sass preprocessor](http://sass-lang.com)), and by default these styles are added to the dom for you! That means you can build a custom skin by simply taking advantage of the cascading aspect of CSS and overriding
the styles you'd like to change.

If you don't want Video.js to inject the base styles for you, you can disable it by setting `window.VIDEOJS_NO_BASE_THEME = false` before Video.js is loaded. Keep in mind that without these base styles
enabled, you'll need to manually include them.

## Icons

You can view all of the icons available in the base theme by renaming and viewing [`icons.html.example`](https://github.com/videojs/video.js/blob/master/sandbox/icons.html.example) in the sandbox directory.

## Customization

When you create a new skin, the easiest way to get started is to simply override the base Video.js theme. You should include a new class matching the
name of your theme, then just start overriding!

```css
.vjs-skin-hotdog-stand { color: #FF0000; }
.vjs-skin-hotdog-stand .vjs-control-bar { background: #FFFF00; }
.vjs-skin-hotdog-stand .vjs-play-progress { background: #FF0000; }
```

This would take care of the major areas of the skin (play progress, the control bar background, and icon colors), but you can skin any other aspect.
Our suggestion is to use a browser such as Firefox and Chrome, and use the developer tools to inspect the different elements and see what you'd like to change and what classes
to target when you do so.

More custom skins will be available for download soon. If you have one you like you can share it by forking [this example on CodePen.io](http://codepen.io/heff/pen/EarCt), and adding a link on the [Skins wiki page](https://github.com/videojs/video.js/wiki/Skins).

{'repo_name': 'rapidpro/rapidpro', 'stars': '653', 'repo_language': 'JavaScript', 'file_name': 'main.yml', 'mime_type': 'text/plain', 'hash': 2775226057221758618, 'source_dataset': 'data'}


importance: 5

---

# Position the note inside (absolute)

Extend the previous task <info:task/position-at-absolute>: teach the function  `positionAt(anchor, position, elem)` to insert `elem` inside the `anchor`.

New values for `position`:

- `top-out`, `right-out`, `bottom-out` -- work the same as before, they insert the `elem` over/right/under `anchor`.
- `top-in`, `right-in`, `bottom-in` -- insert `elem` inside the `anchor`: stick it to the upper/right/bottom edge.

For instance:

```js
// shows the note above blockquote
positionAt(blockquote, "top-out", note);

// shows the note inside blockquote, at the top
positionAt(blockquote, "top-in", note);
```

The result:

[iframe src="solution" height="310" border="1" link]

As the source code, take the solution of the task <info:task/position-at-absolute>.

{'repo_name': 'javascript-tutorial/ko.javascript.info', 'stars': '432', 'repo_language': 'HTML', 'file_name': 'index.html', 'mime_type': 'text/html', 'hash': -8510172945351463469, 'source_dataset': 'data'}


/*
   Copyright The containerd Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package ttrpc

import (
	"context"
	"net"
	"os"
	"syscall"

	"github.com/pkg/errors"
	"golang.org/x/sys/unix"
)

type UnixCredentialsFunc func(*unix.Ucred) error

func (fn UnixCredentialsFunc) Handshake(ctx context.Context, conn net.Conn) (net.Conn, interface{}, error) {
	uc, err := requireUnixSocket(conn)
	if err != nil {
		return nil, nil, errors.Wrap(err, "ttrpc.UnixCredentialsFunc: require unix socket")
	}

	rs, err := uc.SyscallConn()
	if err != nil {
		return nil, nil, errors.Wrap(err, "ttrpc.UnixCredentialsFunc: (net.UnixConn).SyscallConn failed")
	}
	var (
		ucred    *unix.Ucred
		ucredErr error
	)
	if err := rs.Control(func(fd uintptr) {
		ucred, ucredErr = unix.GetsockoptUcred(int(fd), unix.SOL_SOCKET, unix.SO_PEERCRED)
	}); err != nil {
		return nil, nil, errors.Wrapf(err, "ttrpc.UnixCredentialsFunc: (*syscall.RawConn).Control failed")
	}

	if ucredErr != nil {
		return nil, nil, errors.Wrapf(err, "ttrpc.UnixCredentialsFunc: failed to retrieve socket peer credentials")
	}

	if err := fn(ucred); err != nil {
		return nil, nil, errors.Wrapf(err, "ttrpc.UnixCredentialsFunc: credential check failed")
	}

	return uc, ucred, nil
}

// UnixSocketRequireUidGid requires specific *effective* UID/GID, rather than the real UID/GID.
//
// For example, if a daemon binary is owned by the root (UID 0) with SUID bit but running as an
// unprivileged user (UID 1001), the effective UID becomes 0, and the real UID becomes 1001.
// So calling this function with uid=0 allows a connection from effective UID 0 but rejects
// a connection from effective UID 1001.
//
// See socket(7), SO_PEERCRED: "The returned credentials are those that were in effect at the time of the call to connect(2) or socketpair(2)."
func UnixSocketRequireUidGid(uid, gid int) UnixCredentialsFunc {
	return func(ucred *unix.Ucred) error {
		return requireUidGid(ucred, uid, gid)
	}
}

func UnixSocketRequireRoot() UnixCredentialsFunc {
	return UnixSocketRequireUidGid(0, 0)
}

// UnixSocketRequireSameUser resolves the current effective unix user and returns a
// UnixCredentialsFunc that will validate incoming unix connections against the
// current credentials.
//
// This is useful when using abstract sockets that are accessible by all users.
func UnixSocketRequireSameUser() UnixCredentialsFunc {
	euid, egid := os.Geteuid(), os.Getegid()
	return UnixSocketRequireUidGid(euid, egid)
}

func requireRoot(ucred *unix.Ucred) error {
	return requireUidGid(ucred, 0, 0)
}

func requireUidGid(ucred *unix.Ucred, uid, gid int) error {
	if (uid != -1 && uint32(uid) != ucred.Uid) || (gid != -1 && uint32(gid) != ucred.Gid) {
		return errors.Wrap(syscall.EPERM, "ttrpc: invalid credentials")
	}
	return nil
}

func requireUnixSocket(conn net.Conn) (*net.UnixConn, error) {
	uc, ok := conn.(*net.UnixConn)
	if !ok {
		return nil, errors.New("a unix socket connection is required")
	}

	return uc, nil
}

{'repo_name': 'kata-containers/runtime', 'stars': '1951', 'repo_language': 'Go', 'file_name': 'kata-runtime', 'mime_type': 'text/x-shellscript', 'hash': -2070682903946898926, 'source_dataset': 'data'}


<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
               name="org.opencastproject.index.service.resources.list.provider.ServicesListProvider" immediate="true"
               activate="activate">
  <implementation class="org.opencastproject.index.service.resources.list.provider.ServicesListProvider"/>
  <property name="service.description" value="Servers list provider"/>
  <property name="opencast.service.type"
            value="org.opencastproject.index.service.resources.list.provider.ServicesListProvider"/>

  <reference name="serviceRegistry" interface="org.opencastproject.serviceregistry.api.ServiceRegistry"
             cardinality="1..1" policy="static" bind="setServiceRegistry"/>

  <service>
    <provide interface="org.opencastproject.list.api.ResourceListProvider"/>
  </service>

</scr:component>

{'repo_name': 'opencast/opencast', 'stars': '157', 'repo_language': 'Java', 'file_name': 'engage-movies-parallel.properties', 'mime_type': 'text/plain', 'hash': 4273887460771088895, 'source_dataset': 'data'}


\documentclass[12pt]{article}

%opening
\title{Coursera - Machine Learning}
\author{www.Stats-Lab.com}

\begin{document}

\maketitle

\subsection{Naive Bayes Classifier}

\begin{itemize}
\item A Naive Bayes classifier is a simple probabilistic classifier based on applying Bayes' theorem with strong (naive) independence assumptions.\item  A more descriptive term for the underlying probability model would be "independent feature model".
\item  In simple terms, a naive Bayes classifier assumes that the presence (or absence) of a particular feature of a class is unrelated to the presence (or absence) of any other feature, given the class variable.
\item  For example, a fruit may be considered to be an apple if it is red, round, and about 4" in diameter.
\item  Even if these features depend on each other or upon the existence of the other features, a naive Bayes classifier considers all of these properties to independently contribute to the probability that this fruit is an apple
\end{itemize}
\end{document}

{'repo_name': 'DragonflyStats/Coursera-ML', 'stars': '361', 'repo_language': 'TeX', 'file_name': 'normalEqn.m', 'mime_type': 'text/plain', 'hash': 4690002799190380720, 'source_dataset': 'data'}


#!/sbin/openrc-run

name="ferm"
description="For Easy Rule Making is a perl-based iptables frontend"
description_checkconfig="Checks script validity"

extra_commands="checkconfig"

depend() {
    after net sysctl
    provide firewall
}

checkconfig() {
    ferm -n "$inputfile"
}

start_pre() {
    if [ "${RC_CMD}" != "restart" ] ; then
        checkconfig || return $?
    fi
}

stop_pre() {
    if [ "${RC_CMD}" = "restart" ] ; then
        checkconfig || return $?
    fi
}

start() {
    ebegin "Loading $inputfile rules"
    ferm "$inputfile"
    eend $?
}

stop() {
    ebegin "Unloading $inputfile rules"
    ferm -F "$inputfile"
    eend $?
}

{'repo_name': 'alpinelinux/aports', 'stars': '445', 'repo_language': 'Shell', 'file_name': '10-devproc-lxpanel-0.6.2.patch', 'mime_type': 'text/x-diff', 'hash': -3351013285933537257, 'source_dataset': 'data'}


<?xml version="1.0" encoding="utf-8"?>

<!--
  ~ Copyright 2013 serso aka se.solovyev
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  ~
  ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~ Contact details
  ~
  ~ Email: se.solovyev@gmail.com
  ~ Site:  http://se.solovyev.org
  -->

<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android">

    <ListPreference
        a:entries="@array/cpp_simple_theme_names"
        a:entryValues="@array/cpp_simple_themes"
        a:key="widget.theme"
        a:title="@string/cpp_theme" />

</PreferenceScreen>

{'repo_name': 'serso/android-calculatorpp', 'stars': '312', 'repo_language': 'Java', 'file_name': 'Wiki.java', 'mime_type': 'text/x-c', 'hash': 695830746386014026, 'source_dataset': 'data'}


Command Buttons & Monitors
==========================

:download:`commands.htm <../dev/commands.htm>` (hint: right click, save as)

.. literalinclude:: ../dev/commands.htm
   :language: html
   :linenos:

{'repo_name': 'openvehicles/Open-Vehicle-Monitoring-System-3', 'stars': '155', 'repo_language': 'C', 'file_name': 'NL.txt', 'mime_type': 'text/plain', 'hash': -8146507639421845801, 'source_dataset': 'data'}


module foo_free
contains
  subroutine bar13(a)
    !f2py intent(out) a
    integer a
    a = 13
  end subroutine bar13
end module foo_free

{'repo_name': 'ryfeus/lambda-packs', 'stars': '940', 'repo_language': 'Python', 'file_name': 'six.py', 'mime_type': 'text/x-python', 'hash': -9056424556062495065, 'source_dataset': 'data'}