MESSAGE
DATE | 2016-11-09 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Learn] merge sort parallel hw
|
From learn-bounces-at-nylxs.com Wed Nov 9 15:52:17 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id C174A161312; Wed, 9 Nov 2016 15:52:16 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from [10.0.0.62] (flatbush.mrbrklyn.com [10.0.0.62]) by mrbrklyn.com (Postfix) with ESMTP id 255E1160E77; Wed, 9 Nov 2016 15:52:14 -0500 (EST) To: learn-at-nylxs.com, Samir Iabbassen From: Ruben Safir Message-ID: <6714100a-3ebd-8129-b8bc-6d1db3ae59be-at-mrbrklyn.com> Date: Wed, 9 Nov 2016 15:52:14 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------EA1C5475F965B901549E987B" Subject: [Learn] merge sort parallel hw X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
This is a multi-part message in MIME format. --------------EA1C5475F965B901549E987B Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
I can't finish by tonight. I borrowed a mergesort algorithm from the net at here
http://www.cprogramming.com/tutorial/computersciencetheory/merge.html
I ported it to C++ and it failed to work. I changed the name of the vars to more sensible names, and converged some of his math because what was there just seemed to sloppy. I mocked it and merged the last subroutine directly in main and it just failed to work. So I then copied and pasted his work and altered it to make it a complete c program and it does work :(
I can't find what I did wrong. I might have just been better off writing it by myself but I wanted to finish before class tonight, and to focus on the thread library.
Its 4PM and I'm out of time. Attached at the cpp files and the c files
They are all part of the C++ code accept for the one with the c sufix. That one is the copy and paste version. This will take another day at least.
Reuvain
-- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013
--------------EA1C5475F965B901549E987B Content-Type: text/x-c++src; name="main.cpp" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="main.cpp"
/* * ===================================================================================== * * Filename: main.cpp * * Description: merge sort main * * Version: 1.0 * Created: 11/09/2016 09:33:20 AM * Revision: none * Compiler: gcc * * Author: Dr. Fritz Mehner (mn), mehner-at-fh-swf.de * Company: FH Südwestfalen, Iserlohn * * ===================================================================================== */ #include #include "msort.h" #include #include
#define SIZE 20
int main(int argv, char **argc) { using namespace std;
int *data = new int[SIZE]; int *cache = new int[SIZE]; struct timespec now; clock_gettime(CLOCK_REALTIME, &now); long seed = now.tv_nsec; srand( (unsigned int) seed); for(int i = 0; i < SIZE; i++){ data[i] = rand() % 1000; cout << data[i] << "\t"; } cout << endl;
cout << "________________________________________________________" << endl;
for(int i = 0; i < SIZE; i++){ cout << "Before: " << i << " ==> " << data[i] << "\t" << endl; } merge::track(data, 0, SIZE, cache); for(int i = 0; i < SIZE; i++){ cout << "After: " << i << "==> " << data[i] << "\t" << endl; }
return 0;
}
--------------EA1C5475F965B901549E987B Content-Type: text/plain; charset=UTF-8; name="makefile" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="makefile"
Q1hYOj1nKysgCkNYWEZMQUdTOj0tV2FsbCAtZ2dkYiAKCkxERkxBR1M6PS1ML3Vzci9sb2Nh bC9saWIvbXlzcWwgLWxteXNxbHBwIC1sbXlzcWxjbGllbnQKCm1zb3J0IDoJbXNvcnQubyBt YWluLm8KCSR7Q1hYfSAgJHtDWFhGTEFHU30gLW8gbXNvcnQgbXNvcnQubyBtYWluLm8KCm1h aW4ubyA6CW1haW4uY3BwCgkke0NYWH0gICR7Q1hYRkxBR1N9IC1vIG1haW4ubyAtYyBtYWlu LmNwcAkKCm1zb3J0Lm8gOgltc29ydC5jcHAJbXNvcnQuaAoJJHtDWFh9ICAke0NYWEZMQUdT fSAtYyBtc29ydC5jcHAKCmNsZWFuCTogCglybSBub2RlcyAqLm8gbWFrZS5kZXBzCgl0b3Vj aCAqLmNwcCAqLmgKCmluY2x1ZGUgbWFrZS5kZXBzCm1ha2UuZGVwczogKi5jcHAgOyBnY2Mg LU0gKi5jcHAgPiRACg== --------------EA1C5475F965B901549E987B Content-Type: text/x-c++src; name="msort.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="msort.cpp"
#include "msort.h" #include
namespace merge{
int max(int x, int y){ int ret; x>y ? ret=x : ret=y; std::cout << x << ":" << y << "==>" << ret << std::endl; return ret; }
void track(int* in, int left, int right, int* space) { int i = 0; if(right == left + 1){ std::cout << "BACKUP" << std::endl; return; } int length = right - left; std::cout << "length =>" << length << std::endl; int mpt = length/2; std::cout << "mpt =>" << mpt << std::endl; int pos1 = left; int pos2 = left + mpt; int pos3 = right; std::cout << "pos1 =>" << pos1 << " pos2==>" << pos2 << " pos3==>" << pos3 << std::endl; //do the recursion now on the left and right branches std::cout << "LEFT <<==" << std::endl; track(in, pos1, pos2, space); std::cout << "RIGHT ==>>" << std::endl; track(in, pos2, pos3, space); for(i = 0; i < length; i++) { if(pos1 < pos2 && ( pos2 == pos3 || max(in[pos1], in[pos2]) == in[pos1])) { space[i] = in[pos1]; pos1++; } else{ space[i] = in[pos2]; pos2++; } } /* transfer array segment */ for(i = left; i < right; i++) { in[i] = space[i - left]; } }
}//end namespace
--------------EA1C5475F965B901549E987B Content-Type: text/x-chdr; name="msort.h" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="msort.h"
/* * ===================================================================================== * * Filename: msort.h * * Description: Header File for Parrell Msort algorthm * * Version: 1.0 * Created: 11/09/2016 04:28:51 AM * Revision: none * Compiler: gcc * * Author: Ruben Safir * Company: NYLXS Inc * * ===================================================================================== */ #ifndef MSORT_INC #define MSORT_INC
namespace merge{
int max(int x, int y); void track(int* , int , int , int* ); //int sort(int &input , int size);
} #endif /* ----- #ifndef MSORT_INC ----- */
--------------EA1C5475F965B901549E987B Content-Type: text/plain; charset=UTF-8; name="tmp" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="tmp"
MTE5CTQ4MAk5MTQJNjI5CTM1MgkxMTEJOTIzCTkyCTYzNgkxNTIJMjg2CTEwNAkyNDQJNDMy CTYxNwk2NjkJMzY3CTMwOQkxNzQJNDc0CQpfX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fXwpCZWZvcmU6ICAwID09PiAxMTkJCkJlZm9y ZTogIDEgPT0+IDQ4MAkKQmVmb3JlOiAgMiA9PT4gOTE0CQpCZWZvcmU6ICAzID09PiA2MjkJ CkJlZm9yZTogIDQgPT0+IDM1MgkKQmVmb3JlOiAgNSA9PT4gMTExCQpCZWZvcmU6ICA2ID09 PiA5MjMJCkJlZm9yZTogIDcgPT0+IDkyCQpCZWZvcmU6ICA4ID09PiA2MzYJCkJlZm9yZTog IDkgPT0+IDE1MgkKQmVmb3JlOiAgMTAgPT0+IDI4NgkKQmVmb3JlOiAgMTEgPT0+IDEwNAkK QmVmb3JlOiAgMTIgPT0+IDI0NAkKQmVmb3JlOiAgMTMgPT0+IDQzMgkKQmVmb3JlOiAgMTQg PT0+IDYxNwkKQmVmb3JlOiAgMTUgPT0+IDY2OQkKQmVmb3JlOiAgMTYgPT0+IDM2NwkKQmVm b3JlOiAgMTcgPT0+IDMwOQkKQmVmb3JlOiAgMTggPT0+IDE3NAkKQmVmb3JlOiAgMTkgPT0+ IDQ3NAkKbGVuZ3RoID0+M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MTE5OjEwND09PjExOQo5MjM6MTA0PT0+OTIzCjYzNjoxMDQ9PT42MzYKNjY5OjEwND09PjY2 OQpBZnRlcjogMD09PiA5MjMJCkFmdGVyOiAxPT0+IDkxNAkKQWZ0ZXI6IDI9PT4gNjY5CQpB ZnRlcjogMz09PiA2MzYJCkFmdGVyOiA0PT0+IDYyOQkKQWZ0ZXI6IDU9PT4gNjI5CQpBZnRl cjogNj09PiA2MTcJCkFmdGVyOiA3PT0+IDQ4MAkKQWZ0ZXI6IDg9PT4gNDc0CQpBZnRlcjog OT09PiA0MzIJCkFmdGVyOiAxMD09PiAzNjcJCkFmdGVyOiAxMT09PiA0NzQJCkFmdGVyOiAx Mj09PiAzMDkJCkFmdGVyOiAxMz09PiAyODYJCkFmdGVyOiAxND09PiAyNDQJCkFmdGVyOiAx NT09PiAxNTIJCkFmdGVyOiAxNj09PiAxMTkJCkFmdGVyOiAxNz09PiA5MjMJCkFmdGVyOiAx OD09PiA2MzYJCkFmdGVyOiAxOT09PiA2NjkJCg== --------------EA1C5475F965B901549E987B Content-Type: text/x-csrc; name="merge.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="merge.c"
#include "stdlib.h" #include "time.h" #include "stdio.h"
#define SIZE 500
/* Helper function for finding the max of two numbers */ int max(int x, int y) { if(x > y) { return x; } else { return y; } }
/* left is the index of the leftmost element of the subarray; right is one * past the index of the rightmost element */ void merge_helper(int *input, int left, int right, int *scratch) { /* base case: one element */ if(right == left + 1) { return; } else { int i = 0; int length = right - left; int midpoint_distance = length/2; /* l and r are to the positions in the left and right subarrays */ int l = left, r = left + midpoint_distance;
/* sort each subarray */ merge_helper(input, left, left + midpoint_distance, scratch); merge_helper(input, left + midpoint_distance, right, scratch);
/* merge the arrays together using scratch for temporary storage */ for(i = 0; i < length; i++) { /* Check to see if any elements remain in the left array; if so, * we check if there are any elements left in the right array; if * so, we compare them. Otherwise, we know that the merge must * use take the element from the left array */ if(l < left + midpoint_distance && (r == right || max(input[l], input[r]) == input[l])) { scratch[i] = input[l]; l++; } else { scratch[i] = input[r]; r++; } } /* Copy the sorted subarray back to the input */ for(i = left; i < right; i++) { input[i] = scratch[i - left]; } } }
/* mergesort returns true on success. Note that in C++, you could also * replace malloc with new and if memory allocation fails, an exception will * be thrown. If we don't allocate a scratch array here, what happens? * * Elements are sorted in reverse order -- greatest to least */
int mergesort(int *input, int size) { int *scratch = (int *)malloc(size * sizeof(int)); if(scratch != NULL) { merge_helper(input, 0, size, scratch); free(scratch); return 1; } else { return 0; } }
int main(int argc, char** argv) { int *data = (int *)malloc(SIZE * sizeof(int )); struct timespec now; clock_gettime(CLOCK_REALTIME, &now); long seed = now.tv_nsec; srand( (unsigned int) seed); for(int i = 0; i < SIZE; i++){ data[i] = rand() % 1000; printf("%i \t", data[i] ); } printf("\n");
printf ("________________________________________________________\n"); mergesort(data, SIZE); for(int i = 0; i < SIZE; i++){ printf("%i \t", data[i] ); } printf("\n"); return 0; }
--------------EA1C5475F965B901549E987B Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--------------EA1C5475F965B901549E987B--
|
|