bool ClusteringInformation::LoadFromFile(const char* fileName, const char* benchName, HDesign& hd) { FILE* resultFile = fopen(fileName, "r"); char bmName[128]; if (resultFile) { fscanf(resultFile, "%s\n", bmName); if (strcmp(bmName, benchName)) { GLOGERROR(LOGINPLACE, "Information in %s doesn't correspond to the design", fileName); return false; } int lClusterRatio; int lDesiredFinalNumberOfClusters; double lClustersAreaTolerance; int lCurrentNumberOfClusters; if (0 == fscanf(resultFile, "a %d %d %I64X %d\n", &lClusterRatio, &lDesiredFinalNumberOfClusters, &lClustersAreaTolerance, &lCurrentNumberOfClusters) || lClusterRatio != mClusterRatio || lDesiredFinalNumberOfClusters != mDesiredFinalNumberOfClusters || lClustersAreaTolerance != mClustersAreaTolerance) { GLOGERROR(LOGINPLACE, "Information in %s doesn't correspond to the design", fileName); return false; } mCurrentNumberOfClusters = lCurrentNumberOfClusters; LoadClustersFromFile(resultFile, hd); LoadClusteringLogFromFile(resultFile); LoadNetLevelsFromFile(resultFile, hd); LoadCurrTableOfAdjacentNetsFromFile(resultFile); fclose(resultFile); return true; } else { ALERT("Error during opening clustering information import file"); return false; } }
int main(int argc, char **argv) { FILE *fpo; char bigtempbuf[2000]; int k, i, j, filescreated = 1, ccount, comma, flip, flipset, depth = 0, maxdepth = 0; time_t startTime; struct dirent *de = NULL; DIR *d = NULL; long long int nwritten, nread; RLINK *CLIST = NULL, *clist2 = NULL, *temp = NULL, *tmlookup = NULL, *temphead = NULL, *LASTMERGED = NULL; EASY_NODE *fl1; EASY_LIST *FILE_LIST = EasyListCreate(NULL, NULL); // verify parameter count if (argc < 3) { printf("\njoin_clusters v1.00 - joins all .CLU files in a directory based on negative indices found in a cluster, assigns unique ids."); printf("\nauthors: Yevgeniy Gefland"); printf("\n\nUsage:\n\n%s INPUTFOLDER outputfile\n", argv[0]); exit(1); } // open out file fpo = fopen(argv[2], "w"); if (NULL == fpo) { printf("\n\nError, cannot open file '%s' for writing!\n", argv[2]); exit(1); } // read file(s) d = opendir(argv[1]); if (d != NULL) { while ((de = readdir(d)) != NULL) { if (strlen(de->d_name) > 6 && 0 == strcasecmp(".clu", de->d_name + strlen(de->d_name) - 4)) { bigtempbuf[0] = 0; strcat(bigtempbuf, argv[1]); if ('/' != bigtempbuf[strlen(bigtempbuf) - 1]) strcat(bigtempbuf, "/"); strcat(bigtempbuf, de->d_name); printf("\nreading %s...", bigtempbuf); ccount = LoadClustersFromFile(bigtempbuf, NULL); printf("%d clusters\n", ccount); totclusters += ccount; } } closedir(d); } // allocate cluster sturture if (0 == totclusters) return 0; CLIST = (RLINK *)calloc(totclusters, sizeof(RLINK)); if (NULL == CLIST) { printf("\n\nCould not allocate CLIST. Aborting!\n"); exit(1); } // hashseen HASHSEEN = CreateSingleHash(2 * totclusters); if (NULL == HASHSEEN) { printf("\n\nCould not allocate CLIST. Aborting!\n"); exit(1); } // load clusters(s) d = opendir(argv[1]); clustercount = 0; if (d != NULL) { while ((de = readdir(d)) != NULL) { if (strlen(de->d_name) > 6 && 0 == strcasecmp(".clu", de->d_name + strlen(de->d_name) - 4)) { bigtempbuf[0] = 0; strcat(bigtempbuf, argv[1]); if ('/' != bigtempbuf[strlen(bigtempbuf) - 1]) strcat(bigtempbuf, "/"); strcat(bigtempbuf, de->d_name); EasyListInsertHead(FILE_LIST, strdup(bigtempbuf)); } } closedir(d); } /* sort to make deterministic */ EasyListQuickSort(FILE_LIST, name_cmp); /* load */ for (fl1 = FILE_LIST->head; fl1 != NULL; fl1 = fl1->next) { char *strtmp = (char *)EasyListItem(fl1); printf("\nprocessing %s...", strtmp); ccount = LoadClustersFromFile( strtmp, CLIST ); printf("%d clusters\n", ccount); } /* join clusters CHANGED Nov 10, 2010 because of directionality problem detected in pipeline */ finclusters = totclusters; for (i = 0; i < totclusters; i++) { printf("\n%d %.2lf%%", i, i * 100 / (double)totclusters ); LASTMERGED = NULL; for (j = 0; j < CLIST[i].refs; j++) { //printf(" %d",CLIST[i].negidlist[j]); flip = flipset = 0; if (NULL != (clist2 = GetSingleHashItem( HASHSEEN, CLIST[i].negidlist[j]))) { if (0 != clist2->cid && clist2 != (CLIST + i)) { // copy unmarked only and not self if (NULL == LASTMERGED) CLIST[i].next = clist2; else LASTMERGED->next = clist2; clist2->cid = 0; // mark finclusters--; // change hashseen pointers of all linked lists as well for (temp = clist2; temp != NULL; temp = temp->next) { LASTMERGED = temp; for (k = 0; k < temp->refs; k++) { SetSingleHashItem( HASHSEEN, temp->negidlist[k], CLIST + i ); // flip? if (!flipset && temp->negidlist[k] == CLIST[i].negidlist[j]) { flipset = 1; flip = ( temp->negdirlist[k] != CLIST[i].negdirlist[j] ); if (flip) { printf("\nflipped c:%d (%d)!", i, CLIST[i].negidlist[j]); } } } } // flip if needed if (flip) { for (temp = clist2; temp != NULL; temp = temp->next) { for (k = 0; k < temp->refs; k++) { temp->negdirlist[k] = (temp->negdirlist[k] == '\'') ? '\"' : '\''; } for (k = 0; k < temp->reads; k++) { temp->dirlist[k] = (temp->dirlist[k] == '\'') ? '\"' : '\''; } } } } } SetSingleHashItem( HASHSEEN, CLIST[i].negidlist[j], CLIST + i ); } } //printf("\nGot here!"); //exit(1); /* write output and double CHECK for dups */ printf("\n\nWriting final cluster file..."); DestroySingleHash(HASHSEEN); HASHSEEN = CreateSingleHash(2 * totclusters); for (i = 0; i < totclusters; i++) { if (0 != CLIST[i].cid) { // fprintf(fpo,"@"); temphead = CLIST + i; comma = 0; // write refs, while skipping dups in negidlists for (temp = CLIST + i; temp != NULL; temp = temp->next) { for (k = 0; k < temp->refs; k++) { if (NULL != (tmlookup = GetSingleHashItem( HASHSEEN, temp->negidlist[k] ))) { // we got a duplicate but it's already in the same cluster so it's ok if (temphead == tmlookup) { // duplicate in diff clustrs? that shouldn't happen at this point } else { printf("\n\nError: duplicate ids found in clusters! (%d)\n", temp->negidlist[k]); //exit(1); } } if (temphead != tmlookup) { if (comma) { fprintf(fpo, ","); } fprintf(fpo, "%d%c", temp->negidlist[k], temp->negdirlist[k]); TotalRefsWritten++; comma = 1; } SetSingleHashItem( HASHSEEN, temp->negidlist[k], CLIST + i ); } } // write reads depth = 0; for (temp = CLIST + i; temp != NULL; temp = temp->next) { depth++; for (k = 0; k < temp->reads; k++) { fprintf(fpo, ",%d%c", temp->idlist[k], temp->dirlist[k]); TotalReadsWritten++; } } if (depth > maxdepth) { maxdepth = depth; } fprintf(fpo, "\n"); } } fclose(fpo); printf("\nTotal clusters read: %d, after join: %d, total refs(written) %d (%d), total reads(written) %d (%d) maxdepth=%d\n", totclusters, finclusters, TotalRefs, TotalRefsWritten, TotalReads, TotalReadsWritten, maxdepth); return 0; }