#include "validate.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <mpi.h>
 
static int predicate(double real, double imag, void *arg)
{
    if (0.0 < real)
        return 1;
    return 0;
}
 
int main(int argc, char **argv)
{
    const int n = 3000; 
    const int root = 0; 
 
    
 
    int thread_support;
    MPI_Init_thread(
        &argc, (char ***)&argv, MPI_THREAD_MULTIPLE, &thread_support);
 
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 
    
 
    int ldA = 0, ldQ = 0, ldC = 0;
    double *A = NULL, *Q = NULL, *C = NULL;
    if (world_rank == root) {
        srand((unsigned) time(NULL));
 
        
 
        ldA = ((n/8)+1)*8; ldC = ((n/8)+1)*8;
        A = malloc(n*ldA*sizeof(double));
        C = malloc(n*ldC*sizeof(double));
        for (int j = 0; j < n; j++)
            for (int i = 0; i < n; i++)
                A[j*ldA+i] = C[j*ldC+i] = 2.0*rand()/RAND_MAX - 1.0;
 
        
 
        ldQ = ((n/8)+1)*8;
        Q = malloc(n*ldA*sizeof(double));
        for (int j = 0; j < n; j++)
            for (int i = 0; i < n; i++)
                Q[j*ldQ+i] = i == j ? 1.0 : 0.0;
    }
 
    
 
    double *real = malloc(n*sizeof(double));
    double *imag = malloc(n*sizeof(double));
    int *select = malloc(n*sizeof(int));
 
    
    
    
 
 
    
    
 
 
    
    
    
 
 
    
    
 
 
    
 
 
    
 
 
    
 
    printf("Hessenberg reduction...\n");
 
    
 
    printf("Schur reduction...\n");
 
    
 
    int num_selected;
    printf("Selected %d eigenvalues out of %d.\n", num_selected, n);
 
    
    
 
    printf("Reordering...\n");
 
    
 
 
    
 
 
    
 
 
    
 
 
    
 
 
    
 
 
    
 
    MPI_Finalize();
 
    if (world_rank == root) {
 
        
 
        check_residual(n, ldQ, ldA, ldQ, ldC, Q, A, Q, C);
 
        
 
        check_orthogonality(n, ldQ, Q);
    }
 
    
 
    free(A);
    free(C);
    free(Q);
 
    free(real);
    free(imag);
    free(select);
 
    return 0;
}