-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Лабы по Шевченко за последний семестр
- Loading branch information
1 parent
f483f93
commit e497c25
Showing
41 changed files
with
6,708 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
iptables -F | ||
iptables -t nat -F | ||
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE | ||
iptables -t nat -A PREROUTING -i wlan0 -j DNAT -p tcp --dport 2222 --to 192.168.57.102:22 | ||
|
23 changes: 23 additions & 0 deletions
23
Шевченко И. В/Параллельное программирование/Битоническая сортировка/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
CXXFLAGS=-O3 -fopenmp | ||
all:posled parallel pthread openmp intrin | ||
|
||
pthread:pthread.cpp | ||
|
||
openmp:openmp.cpp | ||
|
||
posled:posled.cpp | ||
|
||
intrin:intrin.cpp | ||
$(CXX) -O3 -msse2 intrin.cpp -o intrin | ||
|
||
parallel:parallel.cpp | ||
mpicxx parallel.cpp -o parallel | ||
test:parallel | ||
mpiexec -n 4 ~/.mpi/parallel | ||
clean: | ||
if [ -f posled ];then rm posled;fi | ||
if [ -f parallel ];then rm parallel;fi | ||
if [ -f openmp ];then rm openmp;fi | ||
if [ -f pthread ];then rm pthread;fi | ||
if [ -f intrin ];then rm intrin;fi | ||
|
Binary file added
BIN
+24.1 KB
...нко И. В/Параллельное программирование/Битоническая сортировка/intel_ocl_bitonic_sort.zip
Binary file not shown.
163 changes: 163 additions & 0 deletions
163
Шевченко И. В/Параллельное программирование/Битоническая сортировка/intrin.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <memory.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <emmintrin.h> | ||
|
||
short* a; | ||
int n=1<<21; | ||
int iter=0; | ||
|
||
void o(){ | ||
for(int i=0;i<n;i++)printf("%i ",a[i]); | ||
printf("\n"); | ||
} | ||
//ïðèìåíåíèå ïîëóî÷èñòèòåëÿ k | ||
//k=4, k=2 | ||
void half_cleaner(const int start, const int end, const bool asc, const int k){ | ||
for(int i=start;i<end;i+=k) | ||
for(int j=0;(j<k/2)&&(i+j+k/2<end);++j) | ||
if((a[i+j]<a[i+j+k/2])==asc){ | ||
short t=a[i+j]; | ||
a[i+j]=a[i+j+k/2]; | ||
a[i+j+k/2]=t; | ||
} | ||
iter++; | ||
} | ||
//k=8 | ||
void half_cleaner_8(const int start, const int end, const bool asc){ | ||
for(int i=start;i+7<end;i+=8) | ||
{ | ||
__m64 t = *(__m64*)&a[i]; | ||
__m64 d = *(__m64*)&a[i+4]; | ||
__m64 mn = _mm_min_pi16(t,d); | ||
__m64 mx = _mm_max_pi16(t,d); | ||
if (asc) { | ||
*(__m64*)&a[i]=mx; | ||
*(__m64*)&a[i+4]=mn; | ||
} | ||
else { | ||
*(__m64*)&a[i]=mn; | ||
*(__m64*)&a[i+4]=mx; | ||
} | ||
} | ||
iter++; | ||
} | ||
//k=16 | ||
void half_cleaner_16(const int start, const int end, const bool asc){ | ||
for(int i=start;i+15<end;i+=16) | ||
{ | ||
//êñòàòè a[i] çäåñü âûðàâíåíî ïîä 16, ïîýòîìó ìîæíî áåç u, íî âðåìåíè ýòî íå ïðèáàâëÿåò | ||
__m128i t = _mm_loadu_si128((__m128i*)&a[i]); | ||
__m128i d = _mm_loadu_si128((__m128i*)&a[i+8]); | ||
__m128i mn = _mm_min_epi16(t,d); | ||
__m128i mx = _mm_max_epi16(t,d); | ||
if (asc) { | ||
_mm_storeu_si128((__m128i*)&a[i],mx); | ||
_mm_storeu_si128((__m128i*)&a[i+8],mn); | ||
} | ||
else { | ||
_mm_storeu_si128((__m128i*)&a[i],mn); | ||
_mm_storeu_si128((__m128i*)&a[i+8],mx); | ||
} | ||
} | ||
iter++; | ||
} | ||
//k>16 | ||
void half_cleaner_gt16(const int start, const int end, const bool asc, const int k){ | ||
for(int i=start;i<end;i+=k) | ||
{ | ||
for(int j=0;(j<k/2)&&(i+j+k/2<end);j+=8) | ||
{ | ||
__m128i t = _mm_loadu_si128((__m128i*)&a[i+j]); | ||
__m128i d = _mm_loadu_si128((__m128i*)&a[i+j+k/2]); | ||
__m128i mn = _mm_min_epi16(t,d); | ||
__m128i mx = _mm_max_epi16(t,d); | ||
if (asc) { | ||
_mm_storeu_si128((__m128i*)&a[i+j],mx); | ||
_mm_storeu_si128((__m128i*)&a[i+j+k/2],mn); | ||
} | ||
else { | ||
_mm_storeu_si128((__m128i*)&a[i+j],mn); | ||
_mm_storeu_si128((__m128i*)&a[i+j+k/2],mx); | ||
} | ||
/*for(int j=0;(j<k/2)&&(i+j+k/2<end);++j) | ||
if((a[i+j]<a[i+j+k/2])==asc){ | ||
int t=a[i+j]; | ||
a[i+j]=a[i+j+k/2]; | ||
a[i+j+k/2]=t; | ||
}*/ | ||
} | ||
|
||
} | ||
iter++; | ||
} | ||
//ïðèìåíåíèå ïîëóî÷èñòèòåëÿ k ñ ÷åðåäîâàíèåì íàïðàâëåíèÿ óïîðÿäî÷èâàíèÿ | ||
// k=2 | ||
void half_cleaner_alter(int k){ | ||
bool asc=false; | ||
for(int i=0;i<n;i+=k) | ||
{ for(int j=0;(j<k/2)&&(i+j+k/2<n);++j) | ||
if((a[i+j]<a[i+j+k/2])==asc){ | ||
short t=a[i+j]; | ||
a[i+j]=a[i+j+k/2]; | ||
a[i+j+k/2]=t; | ||
} | ||
asc=!asc; | ||
} | ||
} | ||
//ñîðòèðîâêà áèòîíè÷åñêèì ñëÿíèåì k | ||
void bitonic_sort(int s,int e,bool a,int k) | ||
{ | ||
while(k!=1) { | ||
if(k>16) half_cleaner_gt16(s,e,a,k); | ||
else if(k<8) half_cleaner(s,e,a,k); | ||
else if(k==8) half_cleaner_8(s,e,a); | ||
else if(k==16) half_cleaner_16(s,e,a); | ||
else half_cleaner(s,e,a,k); | ||
k=(k/2); | ||
} | ||
} | ||
|
||
void all_sort() { | ||
//ìîæíî óñêîðèòü, åñëè ñäåëàòü øàã "ïèëû" íå 2, à 16: ïðèìåíÿòü bitonic_sort_16elems îòñþäà: | ||
//github.com/hayamiz/sse-practice-sort/blob/master/sse-practice.c | ||
half_cleaner_alter(2); | ||
for(int k=4;k<=n;k<<=1){ | ||
bool asc=false; | ||
for(int i=0;i+k<=n;i+=k) | ||
{ | ||
bitonic_sort(i,i+k,asc,k); | ||
asc=!asc; | ||
} | ||
} | ||
} | ||
#ifdef __GNUC__ | ||
#define MALLOC(pplist,num)\ | ||
posix_memalign(pplist, 16, sizeof(short) * num) | ||
#else | ||
#include <malloc.h> | ||
#define MALLOC(pplist,num)\ | ||
*(pplist)=_aligned_malloc(sizeof(short) * num, 16) | ||
#endif | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
MALLOC((void**)&a,n); | ||
for(int i=0;i<n;++i)a[i]=rand()%100; | ||
|
||
time_t start=clock(); | ||
all_sort(); | ||
time_t end = clock(); | ||
|
||
bool ok=true; | ||
for(int i=1;i<n;++i)if(a[i-1]>a[i])ok=false; | ||
double workTime = end-start; | ||
workTime /= CLOCKS_PER_SEC; | ||
if(ok)printf("Iter: %i, Calculation takes: %.5f sec\n", iter, workTime); | ||
return 0; | ||
} | ||
|
93 changes: 93 additions & 0 deletions
93
Шевченко И. В/Параллельное программирование/Битоническая сортировка/openmp.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <memory.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
#include <omp.h> | ||
|
||
int a[1<<21]; | ||
int n=1<<21; | ||
int iter=0; | ||
|
||
void o(){ | ||
for(int i=0;i<n;i++)printf("%i ",a[i]); | ||
printf("\n"); | ||
} | ||
//ïðèìåíåíèå ïîëóî÷èñòèòåëÿ k | ||
void half_cleaner(int start, int end, bool asc, int k){ | ||
for(int i=start;i<end;i+=k) | ||
for(int j=0;(j<k/2)&&(i+j+k/2<end);++j) | ||
if((a[i+j]<a[i+j+k/2])==asc){ | ||
int t=a[i+j]; | ||
a[i+j]=a[i+j+k/2]; | ||
a[i+j+k/2]=t; | ||
} | ||
iter++; | ||
} | ||
//ïðèìåíåíèå ïîëóî÷èñòèòåëÿ k ñ ÷åðåäîâàíèåì íàïðàâëåíèÿ óïîðÿäî÷èâàíèÿ | ||
void half_cleaner_alter(int k){ | ||
bool asc=false; | ||
for(int i=0;i<n;i+=k) | ||
{ for(int j=0;(j<k/2)&&(i+j+k/2<n);++j) | ||
if((a[i+j]<a[i+j+k/2])==asc){ | ||
int t=a[i+j]; | ||
a[i+j]=a[i+j+k/2]; | ||
a[i+j+k/2]=t; | ||
} | ||
asc=!asc; | ||
} | ||
} | ||
//ñîðòèðîâêà áèòîíè÷åñêèì ñëÿíèåì k | ||
void bitonic_sort(int s,int e,bool a,int k) | ||
{ | ||
while(k!=1) { | ||
half_cleaner(s,e,a,k); | ||
k=(k/2); | ||
} | ||
} | ||
void all_sort() { | ||
half_cleaner_alter(2); | ||
int k=4; | ||
for(;k<=n/4;k<<=1) | ||
#pragma omp parallel firstprivate(k) shared(a,n) | ||
{ | ||
bool asc=omp_get_thread_num()%2; | ||
#pragma omp for | ||
for(int i=0;i<=n-k;i+=k) | ||
{ | ||
bitonic_sort(i,i+k,asc,k); | ||
asc=!asc; | ||
} | ||
} | ||
|
||
for(;k<=n;k<<=1){bool asc=false; | ||
for(int i=0;i+k<=n;i+=k) | ||
{ | ||
bitonic_sort(i,i+k,asc,k); | ||
asc=!asc; | ||
}} | ||
} | ||
|
||
//bool desc (int i,int j) { return (i>j); } | ||
int main(int argc, char* argv[]) | ||
{ | ||
for(int i=0;i<n;++i)a[i]=rand()%100; | ||
//std::vector<int> myvector (a, a+n); | ||
//sort(myvector.begin(), myvector.begin()+n/2); | ||
//sort(myvector.begin()+n/2, myvector.begin()+n,desc); | ||
//memcpy( a, &myvector[0], sizeof( int ) * myvector.size() ); | ||
|
||
time_t start=clock(); | ||
all_sort(); | ||
time_t end = clock(); | ||
|
||
bool ok=true; | ||
for(int i=1;i<n;++i)if(a[i-1]>a[i])ok=false; | ||
double workTime = end-start; | ||
workTime /= CLOCKS_PER_SEC; | ||
if(ok)printf("Iter: %i, Calculation takes: %.5f sec\n", 4*iter, workTime); | ||
return 0; | ||
} | ||
|
Oops, something went wrong.