Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makefile #2

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bbd786f
makefile
sunminyu Mar 17, 2020
d1e1c3f
Merge pull request #1 from sunminyu/smy
sunminyu Mar 17, 2020
68b1049
yudan test
yudandan-ddd Mar 18, 2020
4fc1741
try
sunminyu Mar 28, 2020
3973be1
tjc smmit
MR-Tong-Stu Mar 28, 2020
6bd682a
tjc 2nd summit
MR-Tong-Stu Mar 28, 2020
214bd05
tjc 3th summit
MR-Tong-Stu Mar 28, 2020
6678c77
tjc 4th summit
MR-Tong-Stu Mar 29, 2020
8ba3c4e
test
sunminyu Mar 29, 2020
40bda25
Merge branch 'master' of https://github.com/sunminyu/CloudComputingLa…
sunminyu Mar 29, 2020
eb03759
Merge branch 'smy' of https://github.com/sunminyu/CloudComputingLabs …
sunminyu Mar 29, 2020
b4dbea1
Merge pull request #2 from sunminyu/smy
sunminyu Mar 29, 2020
0aad687
yudan submit test_report
yudandan-ddd Mar 31, 2020
2d899b7
Create null
yudandan-ddd Mar 31, 2020
7986fde
Add files via upload
yudandan-ddd Mar 31, 2020
fd6eebb
Delete null
yudandan-ddd Mar 31, 2020
8cc5bb5
update Lab1-test-report.md
yudandan-ddd Mar 31, 2020
8bccff7
Merge branch 'master' of https://github.com/sunminyu/CloudComputingLabs
yudandan-ddd Mar 31, 2020
b7c5ac6
change Lab1-test-report.md headline
yudandan-ddd Mar 31, 2020
f54ba93
smy_readme
sunminyu Mar 31, 2020
764b5fd
readme
sunminyu Mar 31, 2020
2254acb
Merge pull request #3 from sunminyu/smy
sunminyu Mar 31, 2020
cc1a56f
machine para
MR-Tong-Stu Mar 31, 2020
07356da
pdf
sunminyu Apr 1, 2020
173a22a
pdf
sunminyu Apr 1, 2020
6ae58cd
Merge pull request #4 from sunminyu/smy
sunminyu Apr 1, 2020
06fe09d
Update README.md
CZJ726 Apr 1, 2020
b8d1ce3
Update Lab1-test-report.md
CZJ726 Apr 1, 2020
0bbceb0
Update Lab1-test-report.md
CZJ726 Apr 1, 2020
a1f5d93
easy_version
sunminyu Apr 28, 2020
179bc85
easy_version smy
sunminyu Apr 28, 2020
cc7daca
Merge pull request #5 from sunminyu/smy
sunminyu Apr 28, 2020
7a8d154
Basic version
MR-Tong-Stu Apr 28, 2020
debef88
picture
sunminyu Apr 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CloudComputingLabs
Submodule CloudComputingLabs added at 68b104
8 changes: 8 additions & 0 deletions Lab1/Advanced_version/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CXXFLAGS+=-O2 -ggdb -DDEBUG
CXXFLAGS+=-Wall -Wextra

all: sudoku_solve

sudoku_solve: main.cc neighbor.cc sudoku_basic.cc
g++ -o $@ $^ -O2 -lpthread

34 changes: 34 additions & 0 deletions Lab1/Advanced_version/Makefile.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Project: soudu
# Makefile created by Dev-C++ 5.11

CPP = g++.exe -D__DEBUG__
CC = gcc.exe -D__DEBUG__
WINDRES = windres.exe
OBJ = main.o neighbor.o sudoku_basic.o
LINKOBJ = main.o neighbor.o sudoku_basic.o
LIBS = -L"D:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"D:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc -g3
INCS = -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"D:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN = soudu.exe
CXXFLAGS = $(CXXINCS) -g3
CFLAGS = $(INCS) -g3
RM = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

main.o: main.cc
$(CPP) -c main.cc -o main.o $(CXXFLAGS)

neighbor.o: neighbor.cc
$(CPP) -c neighbor.cc -o neighbor.o $(CXXFLAGS)

sudoku_basic.o: sudoku_basic.cc
$(CPP) -c sudoku_basic.cc -o sudoku_basic.o $(CXXFLAGS)
10 changes: 10 additions & 0 deletions Lab1/Advanced_version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### 动态输入版本

**运行方法如下:**

1.输入./sudoku_solve n 按回车键其中n为线程数目,在不输入n的情况下默认n为2)。

2.输入./测试文件名 按一次回车键即可运行,测试文件即为当前文件夹中test开头的文件。

3.运行期间可按照 2 中方法动态输入其他需要测试的文件,输入完毕后按回车键等待执行。

172 changes: 172 additions & 0 deletions Lab1/Advanced_version/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include "sudoku.h"

char puzzle[MaxNumPuzzle][128];
char solution[MaxNumPuzzle][N+1];
int board[MaxNumPuzzle][N];
int spaces[MaxNumPuzzle][N];
bool EndInputFlag=false;

int total=0;//���������������
int total_solved = 0;//�Ѿ������������Ŀ
bool (*solve)(int, int, int*, int*) = solve_sudoku_basic;//�����������basic�����������
int numOfWorkerThread=2;//���̵߳���Ŀ��Ĭ��˫�߳�
pthread_t* WorkThreads;
pthread_t AddTaskToQueue;
long int threadID;

int nextPuzzleWillSolve=0;
pthread_mutex_t WorkThreadMutex=PTHREAD_MUTEX_INITIALIZER;

//time

int64_t start=0;
int64_t end=0;

const char outputFileName[10]="outfile";
int64_t now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}

void* read_data(void* args) { //���ļ�����

char *file_name=(char*)malloc(256*sizeof(char));
FILE *fp;
while(fgets(file_name, 256, stdin)) {

if(file_name[0]=='\n') {
EndInputFlag=true;
start = now();//��ʼ��ʱ
printf("please wait...\n");
break;
}

if(file_name[strlen(file_name)-1]=='\n') file_name[strlen(file_name)-1]='\0';

fp = fopen(file_name, "r");

if(fp==NULL) {
printf("%s does not exist.please try again\n",file_name);
continue;
}
while(fgets(puzzle[total],1024,fp)) {
// printf("%s",puzzle[total]);
if(strlen(puzzle[total])>=N) {
++total;
}
}
}
//printf("total:%d\n",total);
}
int recvOnePuzzzle(){
int currentPuzzleID=0;
pthread_mutex_lock(&WorkThreadMutex);
while(nextPuzzleWillSolve>=total)
{ if(EndInputFlag){
pthread_mutex_unlock(&WorkThreadMutex);
return -1;
}
else{
sleep(0);
}
}
currentPuzzleID=nextPuzzleWillSolve;
nextPuzzleWillSolve++;
pthread_mutex_unlock(&WorkThreadMutex);
return currentPuzzleID;
}
void* Thread_solve(void* CurThread) {
long int my_CurThread = (long int) CurThread;

int currentPuzzleID=0;
int *PuzzleHaveSolved=(int*)malloc(MaxNumPuzzle*sizeof(int));//��ǰ�̳̽��������id��
long int CurThreadHaveSolvedNum=0;//��ǰ�߳̽����������Ŀ

while(true){
currentPuzzleID = recvOnePuzzzle();
if(currentPuzzleID==-1)
break;

PuzzleHaveSolved[CurThreadHaveSolvedNum]=currentPuzzleID;
CurThreadHaveSolvedNum++;


int nspaces = input(puzzle[currentPuzzleID],board[currentPuzzleID],spaces[currentPuzzleID]);

if(solve(0,nspaces,board[currentPuzzleID],spaces[currentPuzzleID])) {

++total_solved;

if (!solved(board[currentPuzzleID]));
//assert(0);
} else {
printf("currentPuzzleID:%d;NoSolution: %s", currentPuzzleID,puzzle[currentPuzzleID]);
}

for(int j=0; j<N; ++j) {
solution[currentPuzzleID][j]=board[currentPuzzleID][j]+'0';
}
solution[currentPuzzleID][N]='\0';
if(DEBUG_MODE){
printf("The No.%d puzzle's solution:%s\n",currentPuzzleID,solution[currentPuzzleID]);
}
}
if(DEBUG_MODE){
printf("threadID No.%d have solved:",my_CurThread);
for(int i=0;i<CurThreadHaveSolvedNum;++i){
if(i>0) printf(",");
printf("%d",PuzzleHaveSolved[i]);
}
printf("CurThreadHaveSolvedNum: %d\n",CurThreadHaveSolvedNum);
}
return NULL;
}
void outputToFile() {
FILE *fp = fopen(outputFileName,"w");
for(int i=0; i<total; ++i) {
fprintf(fp,"%s\n",solution[i]);
}
printf("okay,all the solution has been output to 'outfile'\n");
}
int main(int argc, char* argv[]) {


if(argc>=2)
numOfWorkerThread=atoi(argv[1]);//���빤���߳���

init_neighbors();


WorkThreads = (pthread_t *)malloc(numOfWorkerThread*sizeof(pthread_t));

for(threadID=0;threadID<numOfWorkerThread; ++threadID) {
if(pthread_create(&WorkThreads[threadID],NULL,Thread_solve,(void*)threadID)!=0){
perror("pthread_create failed");
exit(1);
}
}

if(pthread_create(&AddTaskToQueue, NULL, read_data,NULL)!=0){
perror("pthread_create failed");
exit(1);
}
pthread_join(AddTaskToQueue,NULL);
for(threadID=0;threadID<numOfWorkerThread; ++threadID) {
pthread_join(WorkThreads[threadID],NULL);
}
end = now();
double sec = (end-start)/1000000.0;
printf("%f sec %f ms each %d\n", sec, 1000*sec/total, total_solved);
free(WorkThreads);
outputToFile();
}

123 changes: 123 additions & 0 deletions Lab1/Advanced_version/neighbor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <assert.h>
#include <stdio.h>
#include <strings.h>

#include "sudoku.h"

#include <algorithm>
int neighbors[N][NEIGHBOR];//neighbors[i][j]��ʾ��i������ĵ�j���ھӵ��±�
//�ڶ�ά�ϱ�Ƿ�����ھ� adjacent[i][j]Ϊtrue��ʾ����[i][j]�Ƿ���[row][col]���ھ�
static void mark_adjacent(bool adjacent[ROW][COL], int row, int col)
{
for (int i = 0; i < NUM; ++i) {//����
adjacent[row][i] = true;
adjacent[i][col] = true;
}
int top = (row/3)*3;//��
int left = (col/3)*3;
adjacent[top][left] = true;
adjacent[top][left+1] = true;
adjacent[top][left+2] = true;
adjacent[top+1][left] = true;
adjacent[top+1][left+1] = true;
adjacent[top+1][left+2] = true;
adjacent[top+2][left] = true;
adjacent[top+2][left+1] = true;
adjacent[top+2][left+2] = true;
}
// ��һά��ͳ�Ʒ�����ھӵ��±�
static void collect_neighbors(const bool adjacent[ROW][COL], int row, int col, int myneighbors[NEIGHBOR])
{
int n = 0;
for (int y = 0; y < ROW; ++y) {
for (int x = 0; x < COL; ++x) {
if (adjacent[y][x] && !(y == row && x == col)) {
//assert(n < NEIGHBOR);
myneighbors[n++] = y*COL + x;
}
}
}
//assert(n == NEIGHBOR);
}
//���������Ϣ
static void print_neighbors(const bool adjacent[ROW][COL], int row, int col, int myneighbors[NEIGHBOR])
{
for (int y = 0; y < ROW; ++y) {
for (int x = 0; x < COL; ++x) {
if (y == row && x == col)//���ڷ������ X
putchar('X');
else
putchar(adjacent[y][x] ? 'o' : '.');//��������������o ,�����������.
}
printf("\n");
}
for (int i = 0; i < NEIGHBOR; ++i) {
printf("%2d, ", myneighbors[i]);
}
puts("\n");
}
void init_neighbors()
{
for (int row = 0; row < ROW; ++row) {
for (int col = 0; col < COL; ++col) {
bool adjacent[ROW][COL];
bzero(adjacent, sizeof adjacent);//ÿ�ζ�Ҫ����
mark_adjacent(adjacent, row, col);//ÿ��������20���ھӣ������Լ���21���� ����81������

int me = row*COL + col;//�õ�һά�±�
collect_neighbors(adjacent, row, col, neighbors[me]);

if (DEBUG_MODE)
print_neighbors(adjacent, row, col, neighbors[me]);
}
}
}

bool solved(int board[N])//�ж��Ƿ������ȷ
{
int (*chess)[COL] = (int (*)[COL])board;
for (int row = 0; row < ROW; ++row) {
// �����
int occurs[10] = { 0 };
for (int col = 0; col < COL; ++col) {
int val = chess[row][col];
//assert(1 <= val && val <= NUM);
++occurs[val];
}

if (std::count(occurs, occurs+10, 1) != NUM)
return false;
}

for (int col = 0; col < COL; ++col) {
//�����
int occurs[10] = { 0 };
for (int row = 0; row < ROW; ++row) {
int val = chess[row][col];
// assert(1 <= val && val <= NUM);
++occurs[val];
}

if (std::count(occurs, occurs+10, 1) != NUM)
return false;
}

for (int row = 0; row < ROW; row += 3) {
//��鹬
for (int col = 0; col < COL; col += 3) {
int occurs[10] = { 0 };
++occurs[chess[row ][col]];
++occurs[chess[row ][col+1]];
++occurs[chess[row ][col+2]];
++occurs[chess[row+1][col]];
++occurs[chess[row+1][col+1]];
++occurs[chess[row+1][col+2]];
++occurs[chess[row+2][col]];
++occurs[chess[row+2][col+1]];
++occurs[chess[row+2][col+2]];
if (std::count(occurs, occurs+10, 1) != NUM)
return false;
}
}
return true;
}
Loading